Hey,
I noticed that i can use :: to access data in my class but i can also use . to acces data in my class.Now i ask whats the difference between :: and . in this case?
Hey,
I noticed that i can use :: to access data in my class but i can also use . to acces data in my class.Now i ask whats the difference between :: and . in this case?
When you create an instance of your class, you use the dot(.) operator to allow that instance to access all member functions and variables belonging to that class or any base classes you may have.
When you define your functions outside of class scope, you use :: to access the class scope.
Last edited by legit; 06-18-2009 at 03:21 AM.
:: is the scope resolution operator. You can use this to resolve a route to a desired scope. Prefixing with just :: will take you from global scope on down through any other types you may have. Is what you probably saw :: used for when accessing members was some static type.
so can you call a method of a class that has no instance, using ::?
As a matter of fact, :: is about namespace resolution, not scope resolution. When a class is defined, a namespace is implicitly defined with the same name (i.e. declaring a class A implicitly defines a namespace named A). All member functions of a class are placed within that namespace, whether they are static or not. That is why a non-static member function func() of class A, when implemented outside the definition of class A, is named A::func().
The . operator is for class (or struct) member access. When an instance of a class is defined, its members are accessed using the . operator. If we assume a class A, an instance of A named x, and a member of class A named m, the syntax x.m access the member m of the instance x. This works whether m is static or non-static. Furthermore, x.m and x.A::m are the same thing. The only difference is that "x." is not required if m is a static member.
The -> operator is like operator . except that the left hand operand is a pointer. So, if p points at an object x (i.e. contains the address of x), p->m and (*p).m are the same thing as x.m.
Very interesting -- I hadn't quite thought of it that way before.
I just wanted to add one note: using syntax like x.A::m is usually not necessary, but it can be if x.m would be ambiguous. For example,
orCode:class Base1 { protected: int m; }; class Base2 { protected: int m; } class Derived : public Base1, public Base2 { public: int get_m() { // just saying "m" is ambiguous; we have to say which one we want return Base1::m; } };
Code:class Base { public: virtual void method() { // do something . . . } }; class Derived : public Base { public: virtual void method() { // do something . . . // now delegate to Base's method to finish the job Base::method(); // just saying "method" would resolve into Derived::method() } };
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.