-
March 9th, 2011, 08:53 AM
#1
initialization lists
i have searched many sources about initialization lists but i can't find a useful source . can't understand what it is and for what it is used .Can Anybody explain it in details or show me a good source? i read from http://www.cprogramming.com/tutorial...lists-c++.html
but couldn't understand
moreover can you explain that code step by step with all details(i dont know copy constructors and assignment operators)..Please don't pass any point .Thanks.
struct Base
{
int x;
Base ( int y, int z ) : x ( y+z )
{
// list above is the same as x=y+z; but it calls the constructor for x instead of the assignment operator
}
};
struct Derived : public Base
{
double d;
Derived ( int y, double d ) : Base ( x, 5 ), d ( d )
{
// Above calls Base constructor and initializes d
// Notice that the d outside parentheses is the struct member,
// the one inside parentheses is the constructor parameter
}
}
-
March 9th, 2011, 10:11 AM
#2
Re: initialization lists
Is English not your first language? The comments pretty much say it all.
initialiser list is for constructing members.
use some code like you have above, compiler it, and test it in a debugger - step through line by line and you can see what is happening.
-
March 9th, 2011, 10:13 AM
#3
Re: initialization lists
The comments in that code block seem to be describing what's going on pretty well, although I do abhor the choice to use the same name (d) for both a member and constructor parameter. What aspect are you having trouble with?
-
March 9th, 2011, 10:35 AM
#4
Re: initialization lists
in that row:
// list above is the same as x=y+z; but it calls the constructor for x instead of the assignment operator
list above is the same as x=y+z. > That's OK.
BUT didn't understand what does " but it calls the constructor for x instead of the assignment " mean? how many process exist here? ?What happens step by step?
-
March 9th, 2011, 11:04 AM
#5
Re: initialization lists
Originally Posted by loves_oi
BUT didn't understand what does " but it calls the constructor for x instead of the assignment " mean? how many process exist here? ?What happens step by step?
Well, the wording would be more appropriate if x were a class type rather than a primitive (int). However, the general idea is that there's a distinction between construction of a variable and assignment to a variable.
This constructs an int with a specified value; the variable never exists with any other value:
This assigns a different value to a pre-existing variable:
Note, for completeness, I should point out that this is actually initialization rather than assignment:
When a class constructor has an initializer list, you control how the class members in the list are initialized. When it doesn't, all members are first default-initialized. Maybe you assign something else to them within the constructor body, but that's assignment, not initialization.
For primitives the difference is negligible. For some class types it may not be; and for const members or reference members, initialization is mandatory because assignment to them is impossible.
-
March 9th, 2011, 11:26 AM
#6
Re: initialization lists
Originally Posted by loves_oi
BUT didn't understand what does " but it calls the constructor for x instead of the assignment " mean? how many process exist here? ?What happens step by step?
It's nothing to worry about really.
You can assign initial values to the object variables either in the constructor body or in the initialization list. The latter is marginally more efficient so it's preferred.
Last edited by nuzzle; March 9th, 2011 at 11:30 AM.
-
March 9th, 2011, 03:12 PM
#7
Re: initialization lists
Originally Posted by Lindley
Well, the wording would be more appropriate if x were a class type rather than a primitive (int). However, the general idea is that there's a distinction between construction of a variable and assignment to a variable.
This constructs an int with a specified value; the variable never exists with any other value:
This assigns a different value to a pre-existing variable:
Note, for completeness, I should point out that this is actually initialization rather than assignment:
When a class constructor has an initializer list, you control how the class members in the list are initialized. When it doesn't, all members are first default-initialized. Maybe you assign something else to them within the constructor body, but that's assignment, not initialization.
For primitives the difference is negligible. For some class types it may not be; and for const members or reference members, initialization is mandatory because assignment to them is impossible.
when x is primitive(int) ,it's OK.How about it would be class type?How will it work?
-
March 9th, 2011, 03:23 PM
#8
Re: initialization lists
Here's the simplest way to demonstrate the difference (probably). Consider this class:
Code:
Class MyClass
{
int value;
public:
MyClass()
: value(0)
{
cout << "Default constructor. (Setting value to 0)\n";
}
MyClass(int value_)
: value(value_)
{
cout << "Int constructor (" << value_ << ").\n";
}
MyClass& operator=(int rhs)
{
value = rhs;
cout << "Assignment operator (" << rhs << ")\n";
}
};
class MyInitializerListClass
{
MyClass x;
public:
MyInitializerListClass()
: x(9)
{}
};
class MyConstructorAssignmentClass
{
MyClass x;
public:
MyConstructorAssignmentClass()
{
x = 9;
}
};
int main()
{
cout << "Constructing with initializer list:\n";
MyInitializerListClass obj1;
cout << "Constructing with assignment:\n";
MyConstructorAssignmentClass obj2;
}
I say this will probably demonstrate the issue because compiler optimizations can do funny things to constructors and assignment operators. So to really understand the logic, compile with optimizations disabled (Debug mode in Visual Studio).
Here we are replacing "int x" with "MyClass x" and demonstrating both forms of constructor.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|