Board index » Visual Studio » Copy constructor !

Copy constructor !

Visual Studio176
Hi,



The default copy constructor simply copies each member variable from the

object passed as a parameter to the member variables of the new object. This

is called member -wise (or shallow copy). However if I provide my own copy

constructor but with no code in it, will a shallow copy still be made?



--

Best regards

Robert


-
 

Re:Copy constructor !

Robby <Robby@discussions.microsoft.com>wrote:

Quote
The default copy constructor simply copies each member variable from

the object passed as a parameter to the member variables of the new

object. This is called member -wise (or shallow copy). However if I

provide my own copy constructor but with no code in it, will a

shallow copy still be made?



No. Your object will be effectively default-constructed, the parameter

will be ignored.

--

With best wishes,

Igor Tandetnik



With sufficient thrust, pigs fly just fine. However, this is not

necessarily a good idea. It is hard to be sure where they are going to

land, and it could be dangerous sitting under them as they fly

overhead. -- RFC 1925





-

Re:Copy constructor !

On Fri, 19 Aug 2005 10:42:02 -0700, "Robby"

<Robby@discussions.microsoft.com>wrote:



Quote
Hi,



The default copy constructor simply copies each member variable from the

object passed as a parameter to the member variables of the new object. This

is called member -wise (or shallow copy).



However if I provide my own copy

constructor but with no code in it, will a shallow copy still be made?



The preferred term in C++ is "member-wise", as opposed to "bit-wise". The

member-wise copying you're talking about does call copy ctors, which may

perform a deep copy, but for raw pointers, of course, it's a shallow copy.

So "member-wise" and "shallow" copying really aren't synonyms. For any

ctor, bases and members that aren't mentioned in the member-initialization

list are default-constructed. There's nothing special about copy ctors

here. If you write a copy ctor, you're responsible for all the copying that

goes on, including chaining to bases; the compiler won't do any of it for

you.



--

Doug Harrison

VC++ MVP

-

Re:Copy constructor !

Robby wrote:

Quote
The default copy constructor simply copies each member variable from the

object passed as a parameter to the member variables of the new object.



The default copy constructor _copy-constructs_ each member. If that what

you meant by "copies", then yes.



Quote
This

is called member -wise (or shallow copy). However if I provide my own copy

constructor but with no code in it, will a shallow copy still be made?



No, if the initialiser list is empty, then every member will be

default-constructed if possible.



V

-

Re:Copy constructor !

Hi, A little cofused may I add.....



What do you mean by:

No, if the initialiser list is empty, then every member will be

default-constructed if possible.



So then you're saying that the code fragment below would reconstruct the

member data (shallow copy):



#include <iostream>

using namespace std;



class SimpleCat

{

public:

SimpleCat();

SimpleCat(const SimpleCat &);

~SimpleCat();

int GetAge(int Decision);

void SetAge(int age, int Decision);



private:

int itsAge_Val;

int itsAge_Ref;

int itsAge_Pointer;

int itsAge_PHeap;

};



SimpleCat::SimpleCat()

{

cout << "Constructor called. \n";

itsAge_Val = 0;

itsAge_Ref = 0;

itsAge_Pointer = 0;

itsAge_PHeap = 0;

}



//Copy constructor with innitializer list empty!

SimpleCat::SimpleCat(const SimpleCat &rhs)

{

//this copy constructor does nothing.

cout << "\n *********Copy constructor does nothing*********\n";

}



SimpleCat::~SimpleCat()

{

cout << "Destructor called \n";

}



void SimpleCat::SetAge(int age,int Decision)

{

itsAge_Val = age;

}

int SimpleCat::GetAge(int Decision)

{

return itsAge_PHeap;

}



void Function0(SimpleCat theCat); //Passing an object by value



int main()

{

int a;



SimpleCat Frisky; //Declare for passing by value

Function0(Frisky); //Call by value a copy of the object

cout << "\nActual value set from constructor: " << Frisky.GetAge(1) <<

"\n";

cin>>a;

}



void Function0(SimpleCat theCat) //Passing by value

{

theCat.SetAge(10,1);

cout << "\nThe value of age in Function0 is: " << theCat.GetAge(1) <<

"\n";

}



What I am trying to understand is if I run the above program, will a member

wise copy be done. REMEMBER I am not talking about a deep copy where used

when members are pointers! I am simply talking about a shallow copy.



So then basically, if we know that a member-wise copy (Or shallow copy) is

made by the default copy constructor when passing an object to a function by

value, THEN if we provide our own copy constructor (with no code in it!) as

shown in the above code fragment, would the default copy constructor be smart

enough create a member shallow copy anyways?



--

Best regards

Robert

--

Best regards

Robert





"Victor Bazarov" wrote:



Quote
Robby wrote:

>The default copy constructor simply copies each member variable from the

>object passed as a parameter to the member variables of the new object.



The default copy constructor _copy-constructs_ each member. If that what

you meant by "copies", then yes.



>This

>is called member -wise (or shallow copy). However if I provide my own copy

>constructor but with no code in it, will a shallow copy still be made?



No, if the initialiser list is empty, then every member will be

default-constructed if possible.



V



-

Re:Copy constructor !

Robby <Robby@discussions.microsoft.com>wrote:

Quote
So then you're saying that the code fragment below would reconstruct

the member data (shallow copy):



[snip]



What I am trying to understand is if I run the above program, will a

member wise copy be done.



No. When calling Function0, the compiler creates a temporary object by

using a copy constructor and passing Frisky as a parameter. However,

your copy constructor is a misnomer, it does not actually copy anything.

Member variables of this temporary object remain uninitialized. GetAge

inside Function0 should return some random value.



Quote
So then basically, if we know that a member-wise copy (Or shallow

copy) is made by the default copy constructor when passing an object

to a function by value, THEN if we provide our own copy constructor

(with no code in it!) as shown in the above code fragment, would the

default copy constructor be smart enough create a member shallow copy

anyways?



What default copy constructor? Since you provided your own, the compiler

does not generate a default one. It does not matter that your

constructor does nothing, the mere fact of its existence is sufficien to

suppress the compiler-generated constructor.



It is common to declare a copy constructor and not implement it at all,

to make the object non-copyable. Like this:



class NonCopyable

{

private:

NonCopyable(const NonCopyable&); // no implementation

NonCopyable& operator=(const NonCopyable&); // no implementation

};



NonCopyable x;

NonCopyable y = x; // fails to compile

NonCopyable z; z = x; // fails to compile



void f(NonCopyable w);

f(x); // fails to compile

--

With best wishes,

Igor Tandetnik



With sufficient thrust, pigs fly just fine. However, this is not

necessarily a good idea. It is hard to be sure where they are going to

land, and it could be dangerous sitting under them as they fly

overhead. -- RFC 1925





-