473,322 Members | 1,405 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.

copy construtor - when should it be used?

it should be written in c++?
May 14 '07 #1
5 1631
MMcCarthy
14,534 Expert Mod 8TB
This question is being moved to the C++ forum.

ADMIN
May 14 '07 #2
gpraghuram
1,275 Expert 1GB
Hi,
You dont need a copy constructor unless u have a pointer member in your class.
If you have a pointer member and dont have a copy constructor, it creates the shallow copy problem.
Thanks
Raghuram
May 14 '07 #3
svlsr2000
181 Expert 100+
Copy constructors are mainly required to pass objects of your class as pass by reference. If you dont have one the compiler would create one for you.
May 14 '07 #4
AdrianH
1,251 Expert 1GB
Hi,
You dont need a copy constructor unless u have a pointer member in your class.
If you have a pointer member and dont have a copy constructor, it creates the shallow copy problem.
Thanks
Raghuram
This is only partially true.

You have a copy constructor to manage resourses. Memory (using a pointer) is one of them. You could have also have a file resource, or other resource which you need to replicate with the copy constructor.


Adrian
May 14 '07 #5
AdrianH
1,251 Expert 1GB
Copy constructors are mainly required to pass objects of your class as pass by reference. If you dont have one the compiler would create one for you.
No, if you pass by reference, you do not invoke the copy constructor. You pass a reference to the original object. It is when you pass by value that the copy constructor is invoked.

The default assignment will also invoke the copy constructor (it also will invoke the destructor prior to the copy constructor if the assigned object has already been initialised).


Adrian
May 14 '07 #6

Sign in to post your reply or Sign up for a free account.

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...
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, ...
11
by: dalu.gelu | last post by:
Hi, can anyone help me by writing a sample code of defining a copy constructor in a class having data member as an object of another class. for eg: class A{ int x; public: A(){ x=6;} };
5
by: Dariusz Bismor | last post by:
Hi, Please help me to understand compiler behavior concerning the following code: class X{    int k; public:    X(){ k=1; }    X( X & model ){       k = model.k;
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
3
by: saxenavaibhav17 | last post by:
Actually at some place I am creating object of outcome class. its taking five argument. i don't want to give 3 and 4 th argument. i want to give 1,2 and 5 th argument. Outcome...
2
by: sarojjena | last post by:
By default the compiler will provide a copy construtor but we write a copy construtor own.
2
by: suman.nandan | last post by:
Hi Experts, In the following code (sorry for using C printf in the code !) : ---------------------------------------------- #include <string> #include<cstdio> using namespace std; int main...
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...
1
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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

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.