| | |
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: 630 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






