When to use "::" and when to use "."

c++
10,968

Solution 1

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 -> } :)

Solution 2

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();

Solution 3

:: 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

Solution 4

:: 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 .

Solution 5

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
Share:
10,968

Related videos on Youtube

Jordan
Author by

Jordan

Updated on September 15, 2022

Comments

  • Jordan
    Jordan over 1 year

    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

    • ildjarn
      ildjarn almost 12 years
      Don't forget -> -- if you don't understand :: vs ., -> is likely to confuse you too. ;-]
    • jxh
      jxh
      :: access scope to static class member or namespace symbol, . for member of an instance.
  • Elliot Bonneville
    Elliot Bonneville almost 12 years
    +1 for giving the same answer, but formatting it nicely.
  • Rob Kennedy
    Rob Kennedy almost 12 years
    But "things inside a class" can also mean static members. Those are accessed with ::. So :: is used for something other than distinguishing namespaces.
  • ildjarn
    ildjarn almost 12 years
    Foo::Bar blah = new Foo::Bar(); is certainly not correct -- new returns a pointer, and -> is used to dereference pointers, not ..
  • McKay
    McKay almost 12 years
    @RobKennedy I have amended the answer to cover this case.
  • McKay
    McKay almost 12 years
    @ildjarn ah, how about this. Is this a better example?
  • McKay
    McKay almost 12 years
    Well said, I love the inclusion of the -> operator as well.
  • ildjarn
    ildjarn almost 12 years
    @McKay : Indeed. ;-] (Note that that wasn't my downvote though.)
  • McKay
    McKay almost 12 years
    @ildjarn yeah, I think it's Rob's It came in at about the time of his comment.
  • Jordan
    Jordan almost 12 years
    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!
  • Rob Kennedy
    Rob Kennedy almost 12 years
    Thanks for re-wording, but it wasn't I who voted you down.
  • abarnert
    abarnert almost 12 years
    +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. :)
  • John Humphreys
    John Humphreys almost 12 years
    Haha, yeah that would be a little on the mean side I think :)
  • Joe Schrag
    Joe Schrag almost 10 years
    +1 for giving a concise, clear answer.