uses of the copy constructor | Bytes (2024)

Home Posts Topics Members FAQ

jimjim

Hello all,

What other reason is there for using the copy constructor apart from copying
data members which are pointers to dynamically allocated objects?

TIA

Oct 5 '05 #1

Subscribe Reply

7 uses of the copy constructor | Bytes (1) 1321 uses of the copy constructor | Bytes (2)

Nan Li

A private copy constructor can help to avoid creating objects in the
stack.

Oct 5 '05 #2

Mike Wahler

"jimjim" <ne*****@blueyo nder.co.uk> wrote in message
news:sF******** ***********@tex t.news.blueyond er.co.uk...

Hello all,

What other reason is there for using the copy constructor apart from
copying data members which are pointers to dynamically allocated objects?

The sole purpose of a copy constructor is to initialize a
newly created object with a copy of the value of another,
already existing object of the same type. If you don't define
one, the compiler will synthesize one for you (which might or
might not do what is needed). So there's no way to 'not use'
a copy constructor. (One might want to prohibit copying;
this is done by making the copy ctor private, but it's
still there (at least semantically; an optimizer might elude
the code if possible, but the program is still required to
behave 'as if' the code were there.)

In the cases where default copy constructor's behavior
(memberwise assignment) is inappropriate, one must write
a custom copy constructor. Your example of a 'deep copy'
is very common, but not the only reason for writing one.
Other examples include anything which cannot be done with
memberwise assignment. The list is theoretically unlimited,
including 'perverse' things that make the operation not really
a 'copy', but probably the most common general theme is 'resource
acquisition and disposition'. Look up Stroustroup's acronym
"RAII" (Resource Acquisition is Initialization) .

-Mike

Oct 5 '05 #3

Mike Wahler

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:pX******** ********@newsre ad2.news.pas.ea rthlink.net...

this is done by making the copy ctor private, but it's
still there (at least semantically; an optimizer might elude

elide

Ack! :-)

-Mike

Oct 5 '05 #4

Ali Çehreli

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:pX******** ********@newsre ad2.news.pas.ea rthlink.net...

In the cases where default copy constructor's behavior
(memberwise assignment)
One more correction: surely you meant "memberwise copy." :)
is inappropriate, one must write
a custom copy constructor.

Ali

Oct 5 '05 #5

Jim Langston

"jimjim" <ne*****@blueyo nder.co.uk> wrote in message
news:sF******** ***********@tex t.news.blueyond er.co.uk...

Hello all,

What other reason is there for using the copy constructor apart from
copying data members which are pointers to dynamically allocated objects?

I could think of one other case, perhaps your class opens a file for output.
You wouldn't want the same file opened twice for output (don't think the
system would let you anyway) so you would either need to open a totally
different file, or somehow write though the other class object or something.
Actually, that'd probably be an interesting problem to get around.

Oct 6 '05 #6

jimjim

The list is theoretically unlimited, including 'perverse' things
that make the operation not really a 'copy'

Can you give me an example of what you mean, please?

TIA

Oct 7 '05 #7

Mike Wahler

"jimjim" <ne*****@blueyo nder.co.uk> wrote in message
news:tA******** ***********@tex t.news.blueyond er.co.uk...

The list is theoretically unlimited, including 'perverse' things
that make the operation not really a 'copy'

Can you give me an example of what you mean, please?

class C
{
int i;
int j;

public:
C() : i(0), j(0) {}
C(const C& rhs) : i(rhs.j), j(rhs.i) {}
};
The copy constructor swaps the values of 'i' and 'j'
members in the constructed object. So the result
isn't a 'copy' of object 'rhs', although the
constructor meets language requirments for being
a 'copy constructor'.

Other possibilities include initializing 'i' and 'j'
to some other unrelated values, or not initializing
them at all. Even more perverse would be 'const_casting'
rhs, and modifying that.

-Mike

Oct 7 '05 #8

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

Similar topics

42 5730

Copy constructors and clones

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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC...

.NET Framework

15 21179

C++: Default Copy Constructor

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 pointers) and for objects types call their default constructor. Any others points i should know?

C / C++

8 2969

explicit call to copy constructor and operator = needed

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 or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr...

C / C++

8 20019

Copy constructor in C#

by: Jesper |last post by:

Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.

C# / C Sharp

10 2542

When copy constructors become handy?

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, we do not need to use the copy-constructor. So depending on the above reasoning I can avoid call by value and return by value for class objects,...

C / C++

8 4291

probelm on copy constructor of a derived class.

by: shuisheng |last post by:

Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}

C / C++

22 3601

Operator overloading and copy constructor. Can't find the error.

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 v);

C / C++

4 987

copy constructor

by: Rahul |last post by:

Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another copy which in itself needs a copy constructor. However i was wondering why can't the existing object be passed as a pointer instead of a...

C / C++

9 2884

conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator)

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 Foo() is included, i believe. };

C / C++

7869

What is ONU?

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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...

General

8306

Maximizing Business Potential: The Nexus of Website Design and Digital Marketing

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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...

Online Marketing

1 7900

The easy way to turn off automatic updates for Windows 10/11

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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...

Windows Server

8176

Discussion: How does Zigbee compare with other wireless protocols in smart home applications?

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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...

General

6555

AI Job Threat for Devs

by: agi2029 |last post by:

Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...

Career Advice

1 5677

Access Europe - Using VBA to create a class based on a table - Wed 1 May

by: isladogs |last post by:

The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...

Microsoft Access / VBA

3806

Trying to create a lan-to-lan vpn between two differents networks

by: TSSRALBI |last post by:

Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

Networking - Hardware / Configuration

3830

Windows Forms - .Net 8.0

by: adsilva |last post by:

A Windows Forms form does not have the event Unload, like VB6. What one acts like?

Visual Basic .NET

1 1404

How to add payments to a PHP MySQL app.

by: muto222 |last post by:

How can i add a mobile payment intergratation into php mysql website.

PHP

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

To disable or enable advertisem*nts and analytics tracking please visit the page.

uses of the copy constructor | Bytes (2024)
Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 6359

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.