473,394 Members | 2,168 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,394 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 1469
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.