So when two objects are declared say class shoe shoe1 and class shoe shoe_copy and then shoe1's variables are set, if the line of code: shoe_copy=shoe1; what would that be doing? After all what is the real data structure of the objects of class shoe?

Recommended Answers

All 12 Replies

shoe_copy=shoe1; will call the assignment operator, not the copy constructor. The copy constructor is called for example when you pass an object to a function by value.

// copy constructor called (notice I pass by value, not by reference)
void f(Shoe s)

yes but what willl shoe_copy=shoe1 do?

yes but what willl shoe_copy=shoe1 do?

Call the copy constructor.

Let me explain:

Shoe shoe1; // calls the constructor
Shoe shoe_copy(shoe1); // copy constructor
Shoe shoe_copy = shoe1; // copy constructor, even if it seems it's the assignment operator;

shoe_copy = shoe1; // assignment operator

but the last example shoe_copy=shoe1; - does that set the adresses equal or what does it physically do to shoe_copy.

It will not modify and addresses but it will set all of the values of shoe_copy to the values of shoe1. It wont call a copy constructor. I have seen this called a "shallow copy" (here)

commented: EXCELLENT link +1

Ohhhh... okay that is probably the most informative clear article I have read on the subject and I thank you for that excellent link.

So when p=q calls the copy constructor is this accessing a compiler-defined ovverloaded version of the = operator?

sorry that last post was badly stated as a question, what I meant was: When p=q calls an overloaded assignment operator and preforms a shallow copy is this done using the automatic copy constructor that is called when an object is initialized to another?
So if i explicitly defined a complex copy constructor and said p=q that used the assignment operator would it preform a complex copy?

No, the copy constructor is used only when called explicitly or when an object is being initialized. You could, however, have the assignment operator call your copy constructor.

No, the copy constructor is used only when called explicitly or when an object is being initialized. You could, however, have the assignment operator call your copy constructor.

Yet another context where copy constructor works:

class C {...};
void f(C); // by value
C c;
f(c); // <= parameter/argument binding.

Oh okay thank you, also how would you create a complex copy constructor? I succeeded in making an explicit shallow copy constructor however in the line "value=c.value;" I have been trying to put *value=*c.value to make a complex copy but it has failed breaking at 0xcccccccccc saying: "Unhandled exception at 0x011518af in copy_constructor_test-complex.exe: 0xC0000005: Access violation writing location 0xcccccccc."
here is the working program without the small edit:

class box{
	int *value;
		value=new int;
	box(box &c)


using namespace std;
int main ()
	box a;
	box b(a);   //calls the shallow copy constructor that makes value point to the same dynamic memory
	cout<<"*b.value after copied from a where *a.value is 5: "<<*b.value<<endl;
	cout<<endl<<"*b.value after *a.value=10; : "<<*b.value<<endl;// this will output 10 because &value of both object versions are the same
	cout<<endl<<endl<<"The change of *a.value has effected *b.value since both object versions of value point to the same dynamic memory. Here are the 2 object versions of value: "<<endl<<" a.value: "<<a.value<<endl<<" b.value: "<<b.value<<endl;
	return 0;

Thank you.

The copy constructor is used when the class contain a data member which is a pointer....
when you say object a=b you did not want to make the pointers in the two objects have the same doing this if ,for example object a has been destroyed there will be an run time error .....

and using the equal sign will not enforce the program to use the copy you have either to call it explicitly or overload the equal sign

try this in the copy constructor
box(box &c)
value= new int;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.