943,647 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 933
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 8th, 2009
0

copy constructors quick question

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Feb 8th, 2009
0

Re: copy constructors quick question

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.
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Feb 8th, 2009
0

Re: copy constructors quick question

yes but what willl shoe_copy=shoe1 do?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Feb 8th, 2009
1

Re: copy constructors quick question

Click to Expand / Collapse  Quote originally posted by CPPRULZ ...
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
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Feb 8th, 2009
0

Re: copy constructors quick question

but the last example shoe_copy=shoe1; - does that set the adresses equal or what does it physically do to shoe_copy.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Feb 8th, 2009
1

Re: copy constructors quick question

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Feb 8th, 2009
0

Re: copy constructors quick question

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Feb 9th, 2009
0

Re: copy constructors quick question

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Feb 9th, 2009
0

Re: copy constructors quick question

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Feb 9th, 2009
0

Re: copy constructors quick question

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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: String:: Help!
Next Thread in C++ Forum Timeline: Not sure what a constructor in a header file is doing...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC