class A
{
public:
A(){ //A(0);}
A(int i) { m = i;}
private:
int m;
}
i think it's illegal, but can anyone tell me why?
Printable View
class A
{
public:
A(){ //A(0);}
A(int i) { m = i;}
private:
int m;
}
i think it's illegal, but can anyone tell me why?
Why do u think so?Quote:
Originally posted by jianmin jin
class A
{
public:
A(){ //A(0);}
A(int i) { m = i;}
private:
int m;
}
i think it's illegal, but can anyone tell me why?
It's legal.
Well...why would you do that?Quote:
Originally posted by jianmin jin
can i call copy constructor in default constructor?
Your example doesn't show a copy constructor. The signature of a copy constructor is: A(const A& a)Quote:
Originally posted by jianmin jin
class A
{
public:
A(){ A(0);}
A(int i) { m = i;}
private:
int m;
}
i think it's illegal, but can anyone tell me why?
If I am not wrong, I believe that you want to call a different constructor within the default constructor, to initialize it's member variable. This cannot be done and you can proof it by using a debugger or simply print out the value of the member variable. This is because the statement A(0) in your default constructor only creates an unnamed temporary instance of A. This instance is immediately destroyed when it leaves the scope of the default constructor. As you may see, it never update the member variable of the original instance.
thanks for ur answers, i did it just to prove whether it's right or wrong ,hehe.
yeah, that's what i want to do, so it's illegal ,right? thanks for urQuote:
Originally posted by Kheun
Your example doesn't show a copy constructor. The signature of a copy constructor is: A(const A& a)
If I am not wrong, I believe that you want to call a different constructor within the default constructor.
good answer
Not quite. We can't really say that it is illegal as this code compiles and runs properly. However, in C++, calling the ctr within another ctr serves no purpose in initialization.Quote:
Originally posted by jianmin jin
yeah, that's what i want to do, so it's illegal ,right? thanks for ur
good answer
FYI. Other OO programming languages, like Java, allow such initialization which is adding to the confusion.
In C++, a better way to share initialization code is to write another function. :cool:
Code:class A
{
public:
A(){ init(0);}
A(int i) { init(i);}
private:
void init(int a) {m = a;}
int m;
}
It's better to use an initialization list, so there is no any need for init() here :)Quote:
Originally posted by Kheun
In C++, a better way to share initialization code is to write another function. :cool:
Code:class A
{
public:
A(){ init(0);}
A(int i) { init(i);}
private:
void init(int a) {m = a;}
int m;
}
Code:class A
{
public:
A() : m(0) {}
A(int i) : m(i) {}
private:
int m;
}
Or, in this particular case:
And I'd make the constructor explicit as well.Code:class A
{
public:
A(int i = 0) : m(i) {}
private:
int m;
}
In this trivial case, where only member variables are set, then yes, the initializer list is fine. For non-trivial cases where other work is done, say calculations or initializations of more complex structures or even devices, then an init() is good standard fun.