constructor initializer list

J

jrefactors

I don't understand the usage of constructor initializer list
and when must it be used. I always do code 1 and never do code 2,
but what's the difference between them, and when to use which.

class Student
{
public:
Student();

private:
int _id;
string _name;
};

//code 1
Student::Student(int id, String name)
{
_id = id;
_name = name;
}

//code 2
Student::Student(int id, String name)
: _id(id),
_name(name)
{
}

Please advise. thanks!!



In C++, what are the differences between a class and a struct?
 
V

Victor Bazarov

I don't understand the usage of constructor initializer list
and when must it be used. I always do code 1 and never do code 2,
but what's the difference between them, and when to use which.
[...]

In C++, what are the differences between a class and a struct?

Both topics are covered in the FAQ. Please take a read. You can
find the FAQ here: http://www.parashift.com/c++-faq-lite/
 
R

Rolf Magnus

I don't understand the usage of constructor initializer list
and when must it be used. I always do code 1 and never do code 2,
but what's the difference between them, and when to use which.

Always use version two if you can. It will initialize the objects with the
parameters you specified, while version 1 will first default-initialize the
object, then assign the provided value to it. In your example, it doesn't
make much of a difference, but for classes that have expensive constructors
or assignment operators, 2 is more efficient. Also, you cannot use verison
2 for const or reference members, since those cannot be assigned to.
class Student
{
public:
Student();

private:
int _id;
string _name;
};

//code 1
Student::Student(int id, String name)
{
_id = id;
_name = name;
}

//code 2
Student::Student(int id, String name)
: _id(id),
_name(name)
{
}

Please advise. thanks!!



In C++, what are the differences between a class and a struct?

The default access permissions and inheritance are private for classes and
public for structs.
 
Y

yvinogradov

and that default-initialization mentioned by Rolf will simply fail to
compile in the first version if any of your class members is of a type
that has no default constructor.
 
Y

yvinogradov

and that default-initialization mentioned by Rolf will simply fail to
compile in the first version if any of your class members is of a type
that has no default constructor.
 
R

Rolf Magnus

Rolf said:
Also, you cannot use verison 2 for const or reference members, since those
cannot be assigned to.

Of course that should have been:

Also, you cannot use version 1 for const or reference members, since those
cannot be assigned to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top