By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

copy construktor

P: n/a
Hello Experts!

I have the definition of class Integer below and a main.
Now to my question in main below I have statement (1) which is Integer m =
k.clone(); but actually as I think
statement(1) and statement(2) and statement(3) does mean exactly the same
thing. I'm I right?
(1)Integer m = k.clone();
(2)Integer m(k); //copy constructor
(3)Integer m = k; //another way of writing a copy constructor

main()
{
Integer k(5);
Integer m = k.clone();
}

class Integer
{
public:
Integer(int n= 0)
{
value = n;
}

Integer clone() const
{
Integer res(value);
return res;
}
private:
int value;
};

Many thanks!

//Tony
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
No, calling your clone creates a temporary Integer object, but
constructing m as a copy of another object, through the copy
constructor, doesn't create a temporary.

-Brian

Jul 23 '05 #2

P: n/a
Hello!

But I think the result will be the same for the three statements.

"BigBrian" <wo**@brianmielke.com> skrev i meddelandet
news:11**********************@z14g2000cwz.googlegr oups.com...
No, calling your clone creates a temporary Integer object, but
constructing m as a copy of another object, through the copy
constructor, doesn't create a temporary.

-Brian

Jul 23 '05 #3

P: n/a
Well, that you can check by compiling and running your code. I guess I
thought you were asking a deeper question.

Jul 23 '05 #4

P: n/a
Tony Johansson wrote:
Hello!

But I think the result will be the same for the three statements.


Well, you asked whether they "mean exactly the same thing", and they don't.
But you're right that the result in your example will be the same.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.