473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

copy construktor

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
4 1468
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
1
by: Tony Johansson | last post by:
Hello!! Assume you have the following AccountForStudent class class AccountForStudent { public: AccountForStudent (long, double); ~AccountForStudent ; long getNumber() const
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.