15

Apologies for a question that I assume is extremely basic.

I am having trouble finding out online the difference between the operator :: and . in C++

I have a few years experience with C# and Java, and am familiar with the concept of using . operator for member access.

Could anyone explain when these would be used and what the difference is?

Thanks for your time

Jordan
  • 531
  • 2
  • 7
  • 17

7 Answers7

27

The difference is the first is the scope resolution operator and the second is a member access syntax.

So, :: (scope resolution) can be used to access something further in a namespace like a nested class, or to access a static function. The . period operator will simply access any visible member of the class instance you're using it on.

Some examples:

class A {
    public:

        class B { };

        static void foo() {}
        void bar() {}
};

//Create instance of nested class B.
A::B myB; 

//Call normal function on instance of A.
A a;
a.bar();

//Call the static function on the class (rather than on an instance of the class). 
A::foo(); 

Note that a static function or data member is one that belongs to the class itself, whether or not you have created any instances of that class. So, if I had a static variable in my class, and crated a thousand instances of that class, there's only 1 instance of that static variable still. There would be 1000 instances of any other member that wasn't static though, one per instance of the class.

One more interesting option for when you come to it :) You'll also see:

//Create a pointer to a dynamically allocated A.
A* a = new A();

//Invoke/call bar through the pointer.
a->bar();

//Free the memory!!! 
delete a;

Dynamic memory can be a little more confusing if you haven't learned it yet, so I won't go into details. Just wanted you to know that you can access members with { :: or . or -> } :)

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • Well said, I love the inclusion of the `->` operator as well. – McKay Jul 11 '12 at 22:30
  • Great answer, Cleared up what I needed to know. I have done a fair bit of C so I am used to the -> syntax, thanks for including it in your answer as well though! – Jordan Jul 11 '12 at 22:36
  • +1. Of course it doesn't explain (or even mention) .* or ->*, but we probably don't want to scare the OP away from C++ this early. :) – abarnert Jul 11 '12 at 23:12
  • Haha, yeah that would be a little on the mean side I think :) – John Humphreys Jul 12 '12 at 02:40
8

in C++ :: is the scope resolution operator. It is used to distinguish namespaces, and static methods, basically any case you don't have an object. Where . is used to access things inside an object.

C# uses the . operator for both of them.

namespace Foo
{
    public class Bar
    {          
        public void Method()
        {
        }

        public static void Instance()
        {
        }

    }
}

in C# you would write code like this:

var blah = new Foo.Bar();
blah.Method();

but the equivalent C++ code would look more like this:

Foo::Bar blah;
blah.Method();

But note that the static method would also be accessed using the scope resolution operator, because you're not referencing an object.

Foo::Bar::Instance();

Where again, the C# code would only use the dot operator

Foo.Bar.Instance();
McKay
  • 12,334
  • 7
  • 53
  • 76
4

:: is for namespaces and static member access. C# uses the dot-operator for namespaces instead.

. is for non-static member access.

Not an exhaustive delineation, but it's the relevant bits that may confuse you in light of C# and Java.

For further information, see

IBM - Scope Resolution Operator

IBM - Dot Operator

Christopher Berman
  • 719
  • 1
  • 5
  • 21
2

:: is the scope resolution operator, so when you are resolving a scope, such as a namespace or class, you use that. For member access, you have .

HRÓÐÓLFR
  • 5,842
  • 5
  • 32
  • 35
1

The scope operator :: may be hard to understand if you don't understand namespaces or classes. A namespace is like a container for the names of various things in your code. They're generally used to disambiguate names that are common across libraries. Say both namespaces std and example have the function foobar(). So the compiler knows which function you want to use, you prepend it as either std::foobar() or example::foobar().

The :: operator can also be used when telling the compiler you want to define a function declared in a class or structure. For instance:

class foobar()
{
  public:
  void hello();
  int number; //assume there is a constructor that sets this to 5
}

void foobar::hello()
{
  cout << "Hello, world!" << endl;
}

The . operator is used when you wish to use a member of a class or structure. For instance:

foobar foo;
foo.hello();
cout << foo.number << endl;

Assuming the class is completed by writing a constructor, the output of this would be expected to be:

Hello, world!
5
Littlegator
  • 385
  • 4
  • 18
0

You use the . operator the same in java, when accessing members of a class once its created in the program. the :: is used in number of cases:

When you define a method in the .h/.cpp of a certain class, write

class::methodName()

either for prototyping it, or implementing it.

Also, if you don't explicitly state what namespace you use, you'd have to to use it

std::cout << "This is the output";

instead of just using cout << "This is the output;

Maybe there are more, but i don't remember right now, my C++ is a bit rusty.

La bla bla
  • 8,558
  • 13
  • 60
  • 109
0

In C++, :: is for identifying scope. This can mean namespace scope or class scope.

Eg.

int x;

namespace N {
    int x;

    struct foo {
        static double x;
        double y;    
    };
    struct bar: public foo {
        double y;
    };
}

int main()
{
    int x; // we have a local, hiding the global name
    x = ::x; // explicitly identify the x in global scope
    x += N::x; // explicitly identify the x in namespace N

    N::foo::x = x; // set the static member of foo to our local integer
    N::foo f;
    f.y = f.x; // the static member is implicitly scoped by the object
    f.y += N::foo::x; // or explicitly scoped

    N::bar b;
    assert(b.x == N::foo::x); // this static member is inherited
    b.y = b.x; // we get N::bar::y by default
    b.N::foo::y = b.y; // explicitly request the hidden inherited one
}

// we need to define the storage for that static somewhere too ...
int N::foo::x (0.0);
Useless
  • 64,155
  • 6
  • 88
  • 132