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
0
Junior Poster in Training
Recommended Answers
Jump to Postshoe_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)
{
}
Jump to Postyes 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
Jump to PostIt 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" (
All 12 Replies
minas1
1
Junior Poster in Training
CPPRULZ
0
Junior Poster in Training
minas1
1
Junior Poster in Training
CPPRULZ
0
Junior Poster in Training
death_oclock
103
Posting Whiz
CPPRULZ
commented:
EXCELLENT link
+1
CPPRULZ
0
Junior Poster in Training
CPPRULZ
0
Junior Poster in Training
death_oclock
103
Posting Whiz
ArkM
1,090
Postaholic
CPPRULZ
0
Junior Poster in Training
MMahmoud
0
Newbie Poster
MMahmoud
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.