注册 登录
编程论坛 C++教室

关于c++ 类中 :: 和 . 的区别

h1187647735 发布于 2015-10-04 11:39, 911 次点击
在引用类的公有数据的时候,我都是用的 class.date 但最近发现,好像 :: 也是可以表示类中的数据或方法,
那么,他们在表示类中成员的时候有什么区别呢,在网上查不到,求各位大神来给我解惑,万分感激
12 回复
#2
hjx11202015-10-04 16:18
::   作用域解析运算符,用来指出函数所属的类
.    类成员运算符句点,类成员函数可通过类对象来调用


#3
TonyDeng2015-10-04 16:32
回复 2楼 hjx1120
還有呢?
#4
hjx11202015-10-04 16:39
回复 3楼 TonyDeng
    ‘::’是带头大哥  
    ‘. ’是跑腿小弟
#5
TonyDeng2015-10-04 16:42
沒那麼簡單
#6
h11876477352015-10-04 20:16
::是运算符中等级最高的,它分为三种:
1)global scope(全局作用域符),用法(::name)
2)class scope(类作用域符),用法(class::name)
3)namespace scope(命名空间作用域符),用法(namespace::name)

这是我在网上查的,其中我感觉第二种用法就和 .  的用法一样,是不是说在这种情况下,两者可以通用呢。
#7
TonyDeng2015-10-04 20:18
以下是引用h1187647735在2015-10-4 20:16:56的发言:

::是运算符中等级最高的,它分为三种:
1)global scope(全局作用域符),用法(::name)
2)class scope(类作用域符),用法(class::name)
3)namespace scope(命名空间作用域符),用法(namespace::name)

这是我在网上查的,其中我感觉第二种用法就和 .  的用法一样,是不是说在这种情况下,两者可以通用呢。


關於第二種,你知道類和對象的區別嗎?
#8
TonyDeng2015-10-04 20:52
這個以及另外兩個(關於構造函數和析構函數)的問題,歸結起來是同一性質的問題,即對“類”和“對象”沒有意識。學面向對象編程的,這是第一要點,居然會有這樣的問題。
#9
TonyDeng2015-10-04 22:33
https://bbs.bccn.net/thread-457933-1-1.html
代碼中那兩個計數器就是::
#10
h11876477352015-10-07 17:04
就是::是在类中用的,而.是在对象中用的,是吗


     类是一个抽象的定义,而对象则是具体的数据,对吗
#11
hjx11202015-10-07 17:13
<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
...

#12
TonyDeng2015-10-07 19:37
以下是引用h1187647735在2015-10-7 17:04:25的发言:

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


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

#13
h11876477352015-10-08 20:24
谢谢了,我懂了

    以前是有一点混淆了
1