the use of ":"

Mar 23, 2011 at 7:29pm
I am trying too read a code amd I encountered such a statement:
class Pointers {
public:
Pointers(LAMMPS *ptr) :
lmp(ptr),
logfile(ptr->logfile) {}
virtual ~Pointers() {}

protected:
LAMMPS *lmp;

};

I do not understand the use of ":" after "Pointers(LAMMPS *ptr)", what does it mean? What does it define?

Any help would be pretty much appreciated.

Regards,
Sina
Mar 23, 2011 at 7:35pm
Basically, it is used after constructors for classes to provide initializer lists for the class members.

Your class presumably has members called lmp and logfile. As you may know, when a class constructor is called, constructors are called for all its member variables. Usually in this situation, the default constructors of the members will be called.

However, if you want to call a constructor of a member variable with arguments, you do so by putting this colon ':' after the constructor.

So your code would result in the member lmp being initialized with the value ptr and the member logfile being initialized with the value ptr->logfile.

Hope this helps.
Mar 23, 2011 at 7:42pm
It does help, thank you very much.
Mar 23, 2011 at 7:46pm
No problem :)
Topic archived. No new replies allowed.