Namespaces :: or . ?

I'm reading a book about C++ programming and I'm a little unsure about one thing, the namespaces. I understand that using for example,

std::cout access cout in the std namespace but when I took these classes in school, we would always use the dot operator "."
Are they interchangeable? They book has for example

1
2
3
4
5
6
7
namespace Records{
        Employee::Employee{
              //blah
        }
        void Employee::promote(int blah){
              //blah
        }


The way I read this is that since we are in the Records namespace, to access something in the Employee namespace we must use the :: to get into the Employee namespace. Would this be correct or am I missing something?
It looks like somewhere earlier in the code you defined a class Employee in namespace Records, e.g. like this:

1
2
3
4
5
6
namespace Records {
  class Employee {
    Employe(); // constructor
    void promote(int blah); // some function.
  };
}


When referring to member function (rather than just calling it) you have to use :: operator. From global namespace:

1
2
3
void Records::Employee::promote(int blah) {
  // blah
}



Or from inside namespace Records:


1
2
3
4
5
namespace Records{
  void Employee::promote(int blah){
    //blah
  }
}


Operator :: specifies from which scope we read the names and definitions. It can b a namespace scope, or class scope, or global scope:

1
2
3
void ::Records::Employee::promote(int blah) {
  // blah
}


I hope that clarifies anything.
Regards,
&rzej
You use the '.' operator if you want to access a function or variable in a class you have an object of. Like this:

1
2
3
int i = 10;
Records::Employee empObj;
empObj.promote(i);


That would call the promote function using the empObj object with the int i as the parameter.
Last edited on
Topic archived. No new replies allowed.