0

What's the difference between doing this:

class_name object_name = something;

and

class_name object_name(something);

From what I read here, both use the copy constructor but I don't understand why that happens and how implicit conversions come into play. How I understood it (before reading about it) was that the first uses the default assignment operator (if not defined) by creating a temp object and then calls the copy constructor but that seems to be wrong. I am asking because I read that when making the copy constructor explicit, the first option will fail even if something is of class_name type, so the two options must be different enough. Also is the assignment operator used (using default or user-defined implementation) on top of the copy constructor in the 1st option or is it just a user-friendly syntax form of calling the copy constructor?

Community
  • 1
  • 1
Veritas
  • 2,150
  • 3
  • 16
  • 40
  • 1
    It depends on what `something` is. See this [gotw](http://www.gotw.ca/gotw/036.htm). But neither involves a call to the assignment operator. – juanchopanza Dec 08 '13 at 13:52

2 Answers2

1

If the copy constructor is explicit, the first form could only be achieved by writing:

class_name object_name = class_name(something);

i.e. explicitly calling the copy constructor.

In the end, though, if the copy constructor is explicit, just use the first form if this is unambiguous (beware the most vexing parse), or for extra c++11 points use the brace initialiser syntax, which can never be ambiguous:

class_name object_name{something};

OR using Herb Sutter's "almost always auto" idea:

auto object_name = class_name{something};

To answer your other question, the assignment or copy-assignment operators are not used here. The copy-assignment operator is used where you are assigning a copy to a previously initialised variable:

class_name object_name;
object_name = class_name(something); // uses class_name& operator= (class_name& other)
  • Nice to know. Why is it that I can't use the first option if the constructor is explicit? I thought making a function explicit means that we restrict conversions. I just don't get what that has to do with our first option considering that "something" has the right type. – Veritas Dec 08 '13 at 14:30
  • For that, please see the answer to this question: http://stackoverflow.com/questions/4153527/explicit-copy-constructor-behavior-and-pratical-uses –  Dec 08 '13 at 14:31
  • -1 The assignment operator is not used here, period. – jrok Dec 08 '13 at 14:51
  • You're right, but try correcting me first before downvoting. –  Dec 08 '13 at 14:57
  • See the guidance here: http://stackoverflow.com/help/privileges/vote-down I am VERY amenable to being corrected, we are all here to learn. –  Dec 08 '13 at 15:01
  • Yea I noticed that too after picking best answer. At first I wasn't sure how the compiler used the "=" and afterwards figured it could just be a different meaning for the sign (as I said in my post). I got confused because I though the first option also converted because I wasn't very aware of all the restrictions of the explicit keyword. – Veritas Dec 08 '13 at 15:20
  • I happily remove downvotes when people fix stuff :) (+1 now). I still don't like your "assignment- or copy-assignment operator" - can you tell the difference? – jrok Dec 08 '13 at 16:24
  • Thank you, good Sir. I can indeed - simply being extra clear that neither is used. –  Dec 08 '13 at 16:26
1

The copy constructor and assignment operator are not the same thing.

   Test(const Test &t)
   {
      std::cout<<"Copy constructor called "<<std::endl;
   }
   Test& operator = (const Test &t)
   {
      std::cout<<"Assignment operator called "<<std::endl;
      return *this;
   }

  Test t1, t2;
  t2 = t1;
  Test t3 = t1;

In this example, the assignment operator is called for t2 = t1 and the copy constructor is called for t3 = t1.

If you make the copy constructor explicit, you have to invoke it like this:

Test t3(t1);