|
-
February 11th, 2004, 03:23 AM
#1
can i call copy constructor in default constructor?
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?
-
February 11th, 2004, 03:49 AM
#2
Re: can i call copy constructor in default constructor?
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?
Why do u think so?
It's legal.
-
February 11th, 2004, 04:24 AM
#3
Re: can i call copy constructor in default constructor?
Originally posted by jianmin jin
can i call copy constructor in default constructor?
Well...why would you do that?
-
February 11th, 2004, 05:38 AM
#4
Re: can i call copy constructor in default constructor?
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?
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, 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.
-
February 11th, 2004, 08:14 PM
#5
thanks for ur answers, i did it just to prove whether it's right or wrong ,hehe.
-
February 11th, 2004, 08:16 PM
#6
Re: Re: can i call copy constructor in default constructor?
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.
yeah, that's what i want to do, so it's illegal ,right? thanks for ur
good answer
-
February 11th, 2004, 08:49 PM
#7
Originally posted by jianmin jin
yeah, that's what i want to do, so it's illegal ,right? thanks for ur
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.
FYI. Other OO programming languages, like Java, allow such initialization which is adding to the confusion.
-
February 11th, 2004, 10:35 PM
#8
In C++, a better way to share initialization code is to write another function.
Code:
class A
{
public:
A(){ init(0);}
A(int i) { init(i);}
private:
void init(int a) {m = a;}
int m;
}
-
February 12th, 2004, 02:25 AM
#9
Originally posted by Kheun
In C++, a better way to share initialization code is to write another function.
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 
Code:
class A
{
public:
A() : m(0) {}
A(int i) : m(i) {}
private:
int m;
}
"UNIX is simple; it just takes a genius to understand its simplicity!"
-
February 12th, 2004, 04:13 AM
#10
Or, in this particular case:
Code:
class A
{
public:
A(int i = 0) : m(i) {}
private:
int m;
}
And I'd make the constructor explicit as well.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 19th, 2004, 02:51 PM
#11
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.
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
|