What is this weird colon-member ( : ) syntax in the constructor?
It's a member initialization list. You should find information about it in any good C++ book.
You should, in most cases, initialize all member objects in the member initialization list (however, do note the exceptions listed at the end of the FAQ entry).
The takeaway point from the FAQ entry is that,
All other things being equal, your code will run faster if you use initialization lists rather than assignment.
What is the member variables list after the colon in a constructor good for?
The most common case is this:
class foo{
private:
int x;
int y;
public:
foo(int _x, int _y) : x(_x), y(_y) {}
}
This will set x
and y
to the values that are given in _x
and _y
in the constructor parameters. This is often the best way to construct any objects that are declared as data members.
It is also possible that you were looking at constructor chaining:
class foo : public bar{
foo(int x, int y) : bar(x, y) {}
};
In this instance, the class's constructor will call the constructor of its base class and pass the values x
and y
.
To dissect the function even further:
TransparentObject::TransparentObject( int w, int x, int y, int z ) :
_someMethod( 0 ),
_someOtherMethod( 0 ),
_someOtherOtherMethod( 0 ),
_someMethodX( 0 )
{
int bla;
int bla;
}
The ::
-operator is called the scope resolution operator. It basically just indicates that TransparentObject
is a member of TransparentObject
. Secondly, you are correct in assuming that the body of the constructor occurs in the curly braces.
UPDATE: Thanks for the answers. May those be called methods? ( I guess no ) and what is the difference of call them within the constructor body
There is much more information on this subject than I could possibly ever give you here. The most common area where you have to use initializer lists is when you're initializing a reference or a const
as these variables must be given a value immediately upon creation.
What does the colon mean in a constructor? [duplicate]
This is a way to initialize class member fields before the c'tor of the class is actually called.
Suppose you have:
class A {
private:
B b;
public:
A() {
//Using b here means that B has to have default c'tor
//and default c'tor of B being called
}
}
So now by writting:
class A {
private:
B b;
public:
A( B _b): b(_b) {
// Now copy c'tor of B is called, hence you initialize you
// private field by copy of parameter _b
}
}
What does a colon following a C++ constructor name do? [duplicate]
This is a member initializer list, and is part of the constructor's implementation.
The constructor's signature is:
MyClass();
This means that the constructor can be called with no parameters. This makes it a default constructor, i.e., one which will be called by default when you write MyClass someObject;
.
The part : m_classID(-1), m_userdata(0)
is called member initializer list. It is a way to initialize some fields of your object (all of them, if you want) with values of your choice.
After executing the member initializer list, the constructor body (which happens to be empty in your example) is executed. Inside it you could do more assignments, but once you have entered it all the fields have already been initialized - either to random, unspecified values, or to the ones you chose in your initialization list. This means the assignments you do in the constructor body will not be initializations, but changes of values.
What is this odd thing after the colon in a C++constructor definition? [duplicate]
It says to initialize m_pThread
to NULL before the code inside the constructor is executed.
Variables after the colon in a constructor [duplicate]
It's a way of invoking the constructors of members of the point3 class. if x,y, and z are floats, then this is just a more efficient way of writing this
point3( float X, float Y, float Z):
{
x = X;
y = Y;
z = Z;
}
But if x, y & z are classes, then this is the only way to pass parameters into their constructors
Does move constructor only affect the memory space pointed to by the pointer member of the class?
And my meaning is right?
Sorry, no. Your move constructor will work as you describe, in that it will 'steal' the contents of the name
and country
member variables from other
, but the copy constructor will not. std::move
does nothing with a const
object and you should remove it from your copy constructor. After all, the operative word is copy
, right?
It's worth noting that you don't need to write your own copy or move constructors at all here ('rule of zero'). The default constructors synthesized by the compiler will work just fine. Oh, and std::move
on a primitive type such as an int
also does nothing - the variable will just be copied whether you include it or not.
Related Topics
Checking If All Elements of a Vector Are Equal in C++
Why Should I Not #Include ≪Bits/Stdc++.H≫
Is It a Good Idea to Typedef Pointers
Why Can't Variables Be Declared in a Switch Statement
How Does Guaranteed Copy Elision Work
#Pragma Once VS Include Guards
Size of Character ('A') in C/C++
What Does T&& (Double Ampersand) Mean in C++11
How to Use Std::Maps With User-Defined Types as Key
How to Install (V142) Build Tools in Visual Studio
What Uses Are There For "Placement New"
When to Use Volatile With Multi Threading
Why Does Dividing Two Int Not Yield the Right Value When Assigned to Double
How to Iterate Over the Elements of an Std::Tuple