473,322 Members | 1,501 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,322 software developers and data experts.

Benefits of copy constructor over assignment operator

Hi all,
Can anybody please tell me that what exactly is the need of
copy constructor giving a real life example where i can not do things
with assgnment operator but can do things i want with copy constructor
only.Thanks in advance. ajay

May 6 '06 #1
4 5009
Ajay wrote:
Hi all,
Can anybody please tell me that what exactly is the need of
copy constructor giving a real life example where i can not do things
with assgnment operator but can do things i want with copy constructor
only.Thanks in advance. ajay


If you want to store your objects in standard containers (vector, list,
map, etc.) they need to be Assignable which in turn requires that they
have a copy constructor.

If you write all your code entirely from scratch then I suppose you can
simulate a copy ctor by constructing an object and immediately assigning
to it, but this doesn't seem very sensible.

Mark
May 6 '06 #2

"Ajay" <aj**********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Hi all,
Can anybody please tell me that what exactly is the need of
copy constructor giving a real life example where i can not do things
with assgnment operator but can do things i want with copy constructor
only.Thanks in advance. ajay


class MyType
{
/* etc */
};

void foo(MyType arg)
{
}

int main()
{
MyType mt;
foo(mt); /* requires that 'MyType' has a
copy constructor */
return 0;
}

Depending upon the design of the 'MyType' type,
the needed copy constructor could be a compiler-
synthesized one (created if no user-defined
constructors are defined), or could need to be
a user-defined one (e.g. 'deep copy' needed).

Also, standard library containers require their
element types to be copyable as well as assignable.
'Copyable' means they must either be built-in
types, or have a (either default or user-defined)
copy constructor.

In my example above, with nothing else inside
the class definition, the compiler-generated
copy constructor will meet the requirement.

-Mike
May 6 '06 #3
On 6 May 2006 11:36:15 -0700, "Ajay" <aj**********@gmail.com> wrote:
Hi all,
Can anybody please tell me that what exactly is the need of
copy constructor giving a real life example where i can not do things
with assgnment operator but can do things i want with copy constructor
only.Thanks in advance. ajay


A copy constructor, like any other constructor, creates an object from
scratch. An assignment operator (at least conceptually) destroys the
current contents of an object and replaces it with a copy of the
assigned contents. To answer your question, you cannot construct an
object with an assignment operator.

Best wishes,
Roland Pibinger
May 6 '06 #4
Hi,

Ajay schrieb:
Can anybody please tell me that what exactly is the need of
copy constructor giving a real life example where i can not do things
with assgnment operator but can do things i want with copy constructor
only.Thanks in advance. ajay


Have a look at this:

http://icu.sourceforge.net/docs/pape..._operator.html

Markus
May 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: away | last post by:
1. When a class defined with a member of pointer type, it's necessary to have a copy constructor and assignment operator. If don't pass objects of such class as value in a function and don't do...
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
1
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a...
2
by: Henrik Goldman | last post by:
Hi, Lets say you have class A which holds all data types as private members. Class B then inherits from A and does *only* include a set of public functions which uses A's existing functions for...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.