copy constructors quick question
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?
CPPRULZ
Junior Poster in Training
91 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
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)
{
}
minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8
yes but what willl shoe_copy=shoe1 do?
CPPRULZ
Junior Poster in Training
91 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
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
minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8
but the last example shoe_copy=shoe1; - does that set the adresses equal or what does it physically do to shoe_copy.
CPPRULZ
Junior Poster in Training
91 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
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?
CPPRULZ
Junior Poster in Training
91 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
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?
CPPRULZ
Junior Poster in Training
91 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
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.
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
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:
box.h
class box{
public:
int *value;
box()
{
value=new int;
*value=0;
};
box(box &c)
{*value=*c.value;};
};
main.cpp
#include<iostream>
#include"box.h"
using namespace std;
int main ()
{
box a;
*a.value=5;
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;
system("pause");
*a.value=10;
cout<<endl<<"*b.value after *a.value=10; : "<<*b.value<<endl;// this will output 10 because &value of both object versions are the same
system("pause");
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;
system("pause");
return 0;
}
Thank you.
CPPRULZ
Junior Poster in Training
91 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0