| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 911 人关注过本帖
标题:关于c++ 类中 :: 和 . 的区别
只看楼主 加入收藏
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:0 
<C++ Primer Plus 6th edition>
有这么一段:
Class Scope
Chapter 9 discusses global (or file) scope and local (or block) scope. Recall that you can
use a variable with global scope anywhere in the file that contains its definition, whereas a
variable with local scope is local to the block that contains its definition. Function names,
too, can have global scope, but they never have local scope. C++ classes introduce a new
kind of scope: class scope.
Class scope applies to names defined in a class, such as the names of class data members
and class member functions. Items that have class scope are known within the class but
not outside the class.Thus, you can use the same class member names in different classes
without conflict. For example, the shares member of the Stock class is distinct from the
shares member of a JobRide class.Also class scope means you can’t directly access members
of a class from the outside world.This is true even for public function members.That
is, to invoke a public member function, you have to use an object:
Stock sleeper("Exclusive Ore", 100, 0.25); // create object
sleeper.show(); // use object to invoke a member function
show(); // invalid -- can’t call method directly
Similarly, you have to use the scope-resolution operator when you define member
functions:
void Stock::update(double price)
{
...
}
In short, within a class declaration or a member function definition you can use an
unadorned member name (the unqualified name), as when sell() calls the set_tot()
member function.A constructor name is recognized when it is called because its name is
the same as the class name. Otherwise, you must use the direct membership operator (.),
the indirect membership operator (->), or the scope-resolution operator (::), depending
on the context, when you use a class member name.The following code fragment illustrates
how identifiers with class scope can be accessed:
class Ik
{
private:
int fuss; // fuss has class scope
public:
Ik(int f = 9) {fuss = f; } // fuss is in scope
void ViewIk() const; // ViewIk has class scope
};
void Ik::ViewIk() const //Ik:: places ViewIk into Ik scope
{
cout << fuss << endl; // fuss in scope within class methods
}
...
int main()
{
Ik * pik = new Ik;
Ik ee = Ik(8); // constructor in scope because has class name
ee.ViewIk(); // class object brings ViewIk into scope
pik->ViewIk(); // pointer-to-Ik brings ViewIk into scope
...

2015-10-07 17:13
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
以下是引用h1187647735在2015-10-7 17:04:25的发言:

就是::是在类中用的,而.是在对象中用的,是吗


     类是一个抽象的定义,而对象则是具体的数据,对吗


授人以渔,不授人以鱼。
2015-10-07 19:37
h1187647735
Rank: 2
来 自:湖北huang'g
等 级:论坛游民
帖 子:26
专家分:17
注 册:2014-11-19
收藏
得分:0 
谢谢了,我懂了

    以前是有一点混淆了

努力学习   天天向上
2015-10-08 20:24
快速回复:关于c++ 类中 :: 和 . 的区别
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017231 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved