Thread: Whats the difference beween :: and .

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    8

    Whats the difference beween :: and .

    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?

  2. #2
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    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.

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    :: 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.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    so can you call a method of a class that has no instance, using ::?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Tom_Arch View Post
    so can you call a method of a class that has no instance, using ::?
    Only if the method is static.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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,
    Code:
    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;
        }
    };
    or
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Replies: 10
    Last Post: 11-06-2005, 09:29 PM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM