copy constructors quick question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

copy constructors quick question

 
0
  #1
Feb 8th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: copy constructors quick question

 
0
  #2
Feb 8th, 2009
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)
{
}
Last edited by minas1; Feb 8th, 2009 at 5:41 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: copy constructors quick question

 
0
  #3
Feb 8th, 2009
yes but what willl shoe_copy=shoe1 do?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: copy constructors quick question

 
1
  #4
Feb 8th, 2009
Originally Posted by CPPRULZ View Post
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: copy constructors quick question

 
0
  #5
Feb 8th, 2009
but the last example shoe_copy=shoe1; - does that set the adresses equal or what does it physically do to shoe_copy.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: copy constructors quick question

 
1
  #6
Feb 8th, 2009
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)
Last edited by death_oclock; Feb 8th, 2009 at 9:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: copy constructors quick question

 
0
  #7
Feb 8th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: copy constructors quick question

 
0
  #8
Feb 9th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: copy constructors quick question

 
0
  #9
Feb 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: copy constructors quick question

 
0
  #10
Feb 9th, 2009
Originally Posted by death_oclock View Post
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:
  1. class C {...};
  2. void f(C); // by value
  3. ...
  4. C c;
  5. f(c); // <= parameter/argument binding.
Last edited by ArkM; Feb 9th, 2009 at 6:07 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC