copy construktor

T

Tony Johansson

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
 
B

BigBrian

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
 
T

Tony Johansson

Hello!

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

BigBrian

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

Rolf Magnus

Tony said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top