This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Initializing in constructors, best practice?
I'm new to C++.
Suppose we have this class definition:
Class MyClass {
int a;
int b;
//....
}
I would like to know which is the difference between the two class contructors:
public:
MyClass(int a, int b) : a(a), b(b) {}
and (i would say Java-style):
MyClass(int a, int b) {
this->a = a;
this->b = b;
}
I suppose the first is better in C++; right? why?
The first one (using initializer list) initializes the data members to the given values. The second one initializes them first, then assigns them the values. That is the reason the first is prefered. There is no unnecessary assignment operation there.
This is particularly important when your data members are expensive to construct and/or assign to. Also bear in mind that some types are not default constructable, making it mandatory to use the initializer list.
Related
This question already has answers here:
Implicit constructor argument conversion in C++11
(3 answers)
Closed 3 years ago.
class A{
public:
A(int i){}
};
class B{
public:
B(A){}
};
void test(B){};
int main() {
A a=1;
B b1=a;
test(1);
}
I have two classes, where the first class has a converting int Ctor, which means that I can construct it by "assigning" an integer (first line).
The second class has a converting Ctor with argument type A. It is possible to construct b1 with a (second line).
Is there any possibility to use multiple converting ctor in a chain, in such a way that the third row will compile?
You can explicitly construct B from int, but not implicitly:
test(B{1}); // ok
test(1); // Not ok
That being said, this half-way measure also works:
test({1}); // ok
And everything above works for only one implicitly generated middle class (A). Such a chain can not be further extended.
This question already has answers here:
In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?
(12 answers)
Closed 7 years ago.
While I was working in c++ I see two type of ctor definition.. Is there any difference while assigning value? Does one of them has advantages or just writing style ?
1st ctor definition:
class X
{
public:
X(int val):val_(val){}
private:
int val_;
};
2nd ctor definition:
class X
{
public:
X(int val)
{
val_ = val;
}
private:
int val_;
};
Technically yes, although you can typically not observe any difference for built-in types like int.
The difference is that your first snippet copy-constructs val_ from val, while the second one default constructs val_ and then assigns val to it. As I said above, this usually only matters for more complex types whose constructors actually do work.
A simple example which demonstrates the difference would be
class X
{
public:
X(int val):val_(val){}
private:
const int val_;
};
which compiles vs.
class X
{
public:
X(int val)
{
val_ = val;
}
private:
const int val_;
};
which does not.
Yes there is a difference. In a constructor initializer list, the member variables are constructed, while the assignments in the constructor body is assignments to already constructed variables.
For the basic types like int or float it's no actual difference though.
In your example, there is no difference. Since your member is an int, the two statements have the same effect.
If your member variable was an object, the first form would be better, because it would simply call the constructor of the object class, while the second form would create an object by using the default constructor, then create another, store it into a temporary, call the destructor on the first object, copy the temporary into your member variable, call the destructor again on the second object.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why should I prefer to use member initialization list?
Class A has a member variable i. i can be initialized or assigned during object creation.
A) Initialise
class A {
int i;
public:
A(int _i) : i(_i){}
}
B) assign
class A {
int i;
public:
A(int _i) : { i = _i}
}
My question is what is the basic difference between these 2 approach?
The difference lies in which C++ mechanism is used to initialize i in your class. Case (A) initializes it via constructor, and case (B) uses the assignment operator (or a copy constructor if no assignment operator is defined).
Most C++ compilers would generate exactly the same code for this particular example, because you're using int, which is a "plain old data" type. If i were a class type, it could make a great deal of difference.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does a colon following a C++ constructor name do?
Class construction with initial values
I saw code that looked like this:
class Demo
{
Joystick joystick;
public:
Demo(void):
joystick(1) // The first USB port
{
/* snip */
}
};
Joystick is being initialized before the bracket in the constructor. What does it mean when you do that? What is this called? I'm assuming it differs in some way then initializing joystick inside the bracket -- in what ways does it differ?
It is called an initializer list, and it does differ from initializing inside the body of the constructor.
You can call the constructors of every data member in the class in the initializer list. Also you can call a custom parent class(s) constructor within it, if you didn't, every data member or parent class you don't initialize with the initializer list will be initialized with its default constructor, if it doesn't have, you will see a compiler error.
This is an extended example:
class Parent
{
bool b;
public:
Parent(bool B): b(B)
{
}
};
class Child: public Parent
{
int i;
double d;
public:
Child(int I, double D, bool B): i(I), d(D), Parent(B)
{
}
};
For the order they are called, see this question and this question.
In fact explaining it is an entire article as it's a basic and important thing in classes, just try Googling it and reading some results.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ initialization lists
What is the difference between member-wise initialization and direct initialization in a class?
What is the difference between the two constructors defined in the class?
class A
{
public:
int x;
int y;
A(int a, int b) : x(a), y(b)
{}
A(int a, int b)
{
x = a;
y = b;
}
};
The theoretical answers have been given by other members.
Pragmatically, member-wise initialization is used in these cases :
You have a reference attribute (MyClass & mMyClass) in your class. You need to do a member-wise initialization, otherwise, it doesn't compile.
You have a constant attribute in you class (const MyClass mMyClass). You also need to do a member-wise initialization, otherwise, it doesn't compile.
You have an attribute with no default constructor in your class (MyClass mMyClass with no constructor MyClass::MyClass()). You also need to do a member-wise initialization, otherwise, it doesn't compile.
You have a monstruously large attribute object (MyClass mMyClass and sizeof(MyClass) = 1000000000). With member-wise initialization, you build it only once. With direct initialization in the constructor, it is built twice.
First one uses initialization and second one does NOT use initialization, it uses assignment. In the second one, the members x and y are default-initialized first (with zero), and then they're assigned with a and b respectively.
Also note that in the second one, members are default initialized only if the types of members are having non-trivial default constructor. Otherwise, there is no initialization (as #James noted in the comment).
Now see this topic to know:
What is a non-trivial constructor in C++?