function extends

H

hurcan solter

What is that means by putting a list of functions like follows:
: m_eDemoSetup(eDemoSetup)
, m_pActiveGroup(NULL)
, m_pPopupGroup(NULL)

====================================
CRemoteDemo::CRemoteDemo(EDemoSetup eDemoSetup)
: m_eDemoSetup(eDemoSetup)
, m_pActiveGroup(NULL)
, m_pPopupGroup(NULL)
{

// .... etc function definition

}

it's called constructor initializer list and they are not functions,
it's a special syntax that allows you to initialize members of the
class before the body of the constructor executes.
 
W

worlman385

What is that means by putting a list of functions like follows:
: m_eDemoSetup(eDemoSetup)
, m_pActiveGroup(NULL)
, m_pPopupGroup(NULL)

====================================
CRemoteDemo::CRemoteDemo(EDemoSetup eDemoSetup)
: m_eDemoSetup(eDemoSetup)
, m_pActiveGroup(NULL)
, m_pPopupGroup(NULL)
{

// .... etc function definition
}
 
S

Shobhit Gupta

it's called constructor initializer list and they are not functions,
it's a special syntax that allows you to initialize members of the
class before the body of the constructor executes.

Its equivalent to
CRemoteDemo::CRemoteDemo(EDemoSetup eDemoSetup)
{
m_eDemoSetup = eDemoSetup;
m_pActiveGroup = NULL;
m_pPopupGroup =NULL;
}


You can always choose between "the ordinary way" or "member
initialization list way" of initializing variables.

But in some cases you can initialize variables using "member
initialization lists" only.

Those cases are:
1. const members
2. reference members
3. instances of classes whose constructor requires arguments for
initialization
 
R

red floyd

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!!

Consider the case where you have a const member or a reference member:

#include <string>
class Foo {
private:
const int x;
const std::string& str;
Foo(int x_, const std::string& str_);
};

Foo::Foo(int x_, const std::string& str_)
{
x = 7; // illegal -- x is const
str = str_; // illegal -- assign to const ref,
// plus doesn't seat ref the way you think
}

You have to do:

Foo::Foo(int x_, const std::string& str) : x(x_), str(str_)
{
}
 
W

worlman385

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!!

http://www.velocityreviews.com/forums/t290200-constructor-initializer-list.html
 
J

James Kanze

Its equivalent to
CRemoteDemo::CRemoteDemo(EDemoSetup eDemoSetup)
{
m_eDemoSetup = eDemoSetup;
m_pActiveGroup = NULL;
m_pPopupGroup =NULL;
}

No it's not. The first will call the constructor m_eDemoSetup
with eDemoSetup; the second will call the default constructor of
m_eDemoSetup, then call the assignment operator with eDemoSetup.
It's a major difference.
You can always choose between "the ordinary way" or "member
initialization list way" of initializing variables.

No. In practice, you almost always want to (and very often have
to) use initialization lists.
But in some cases you can initialize variables using "member
initialization lists" only.
Those cases are:
1. const members
2. reference members
3. instances of classes whose constructor requires arguments for
initialization

Case three covers a lot of ground, don't you think?

Another obvious case is if the object doesn't support
assignment. And of course, for base classes.

For non-class types, it's generally considered preferable to use
initialization lists so that the object will be initialized
before you enter into the constructor body (which reduces the
chances of accidentally using it before it is initialized.
 
J

James Kanze

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.

The difference is that initializer lists construct the subobject
with the desired value; assignment in the constructor body first
constructs the subobject with the default constructor (or leaves
it uninitialized in the case of PODS), then assigns a value to
it in the constructor body.

In general, use initializer lists unless there is some very,
very strong reason not 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

Staff online

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top