| | |
copy constructors quick question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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)
{
}
// 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.
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
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
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.
•
•
Join Date: Aug 2008
Posts: 91
Reputation:
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?
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.
c++ Syntax (Toggle Plain Text)
class C {...}; void f(C); // by value ... C c; f(c); // <= parameter/argument binding.
Last edited by ArkM; Feb 9th, 2009 at 6:07 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: String:: Help!
- Next Thread: Not sure what a constructor in a header file is doing...
Views: 627 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






