Reputation: 2688
The below is an exam question:
/* 1 */ string s1 = "FRED";
/* 2 */ string s2 = s1;
/* 3 */ string s3 = "DERF";
/* 4 */ s3 = s1;
For the second and the fourth lines above, name what type of statement that line is. Will they call the same member function of class string? [5 marks]
So my answer:
copy constructor
to copy over all the member variables from s1 into the s2.assignment operator (operator=)
to do so.Therefore they both are calling 2 different member functions.
Is the above an acceptable answer? Is there anything I can add of do I have the wrong idea?
Upvotes: 1
Views: 129
Reputation: 361722
.Line 4 is also using the copy constructor to copy over all the member variables from s1 into s2
No. Line 4 is invoking copy-assignment operator, not the copy-constructor.
A constructor is invoked when an object is being initialized in its declaration. Line 4 doesn't declare anything, so it invokes copy-assignment which is operator=
. Even if the class doesn't overload operator=
, the compiler will generate one for you, and then it will be invoked. So in any case, the line 4 cannot invoke copy-constructor.
Note that you can disable operator=
for your class if you think the copy-assignment of objects of your class doesn't make sense. To disable this, all you need to do is, declare the operator=
in the private section of your class, and then don't provide any definition for it. Something like this:
struct A
{
A(A const &) {} //copy through copy-constructor is allowed
private:
A& operator=(A const &);//copy through copy-assignment is disallowed
};
A x
A y(x); //ok - copy-constructor is allowed
y = x; //error - copy-assignment is disallowed
Likewise, you can disable both : copy-constructor and copy-assignment. For example, the standard library has disabled copy-semantics for all stream classes.
In C++11, you can disable copy-semantic explicitly as:
struct B
{
private:
B(B const &) = delete;
B& operator=(B const &) = delete;
};
Copy-semantic is disabled for class B
, and the usage of =delete
makes it explicit. It also increases the readability of the code and reveals the programmer's intention clearly.
Upvotes: 2
Reputation: 63882
You cannot be certain that it actually makes use of the copy constructor, and it absolutely shouldn't.
Why create a temporary object inside std::string::operator=
when you could copy the data from s1
into s3
directly?
What you wrote about operator=
is correct, but leave out what you wrote about the copy-constructor there. There is no initialization of a new object, therefor no constructor should be called.
You should also leave out the statement regarding making copies of all the member variables, that doesn't have to be true.
And when it comes to objects such as std::string
there are pointers as members that aren't copied, but the memory where they point are being copied into a new memory space (to be used by the destination object).
OP just changed his post deleting what I made a remark on in the first part of this post.
Upvotes: 3