J
Jef Driesen
Hello,
Suppose I have a class that looks like this:
class point {
protected:
unsigned int m_x, m_y; // OR unsigned int m_data[2];
public:
point();
point(unsigned int x, unsigned int y);
point(const point& p);
point& operator= (const point& p);
};
I know it is preferable to use initialization lists to initialize member
variables in a constructor:
point:
oint(unsigned int x, unsigned int y)
: m_x(x), m_y(y) {}
{
}
over assignments within the constructor body:
point:
oint(unsigned int x, unsigned int y)
{
m_x = x;
m_y = y;
}
In the second example, the default constructor for each data member is
invoked prior to the assignment in the constructor body. This will
result in lower performance (overhead of calling the unnecessary default
constructor).
But what if I replace the data members with an array? Now I can only use
assignment in the constructor body:
point:
oint(unsigned int x, unsigned int y)
{
m_data[0] = x;
m_data[1] = y;
}
My questions are:
(1) How is m_data initialized *before* the assignment in the body?
(2) Will this result in lower performance?
Suppose I have a class that looks like this:
class point {
protected:
unsigned int m_x, m_y; // OR unsigned int m_data[2];
public:
point();
point(unsigned int x, unsigned int y);
point(const point& p);
point& operator= (const point& p);
};
I know it is preferable to use initialization lists to initialize member
variables in a constructor:
point:
: m_x(x), m_y(y) {}
{
}
over assignments within the constructor body:
point:
{
m_x = x;
m_y = y;
}
In the second example, the default constructor for each data member is
invoked prior to the assignment in the constructor body. This will
result in lower performance (overhead of calling the unnecessary default
constructor).
But what if I replace the data members with an array? Now I can only use
assignment in the constructor body:
point:
{
m_data[0] = x;
m_data[1] = y;
}
My questions are:
(1) How is m_data initialized *before* the assignment in the body?
(2) Will this result in lower performance?