6

We can create an object in two ways:

myClass myObject = myClass(123);
//or
myClass myObject(123);

Are there any differences in background between these two? I want to use the first one but it seems like combining these two lines:

myClass myObject;
myObject= myClass(123);

Does the second one also do the same thing?

1
  • 3
    No difference once compiled. Feb 21, 2015 at 22:03

3 Answers 3

10
myClass myVariable = myClass(123);

is copy initialization.

myClass myVariable(123);

is direct initialization.

myClass myVariable;
myVariable = myClass(123);

is default initialization followed by copy (or move) assignment.

Typically, the first two are identical because of copy elision. The relevant rule can be found in [class.copy]/31 (N4140, C++14 draft standard):

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object [...]:

— when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

3
  • Thanks. I'd better get used to use direct initialization because the reference says that: Copy-initialization is less permissive than direct-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions. Feb 21, 2015 at 22:22
  • Thanks ! And to complete the picture, [class.expl.init]/1 tells us that "An object of class type can be initialized with a parenthesized expression-list, where the expression-list is construed as an argument list for a constructor that is called to initialize the object. Alternatively, a single assignment-expression can be specified as an initializer using the = form of initialization. Either direct-initialization semantics or copy-initialization semantics apply
    – Christophe
    Feb 21, 2015 at 22:35
  • 1
    The first two are far from identical if the class doesn't support copy; in this case, the first won't compile. Feb 21, 2015 at 23:23
1

Not that I know of, in the end. You have described three ways of defining an object and all 3 ways place the object on top of the stack, calling the same constructor. The functionality would be different if you used the new operator.

6
  • 1
    They are not all equivalent, and the new operator is irrelevant here, it wouldn't change what constructors or special member functions get called.
    – David G
    Feb 21, 2015 at 22:12
  • The two first are equivalent the third one is different.
    – Christophe
    Feb 21, 2015 at 22:13
  • 1
    @Christophe The first two are not equivalent if myClass has an explicit copy/move-constructor.
    – David G
    Feb 21, 2015 at 22:14
  • 1
    @Christophe Or in the far more common case where myClass doesn't support copy. Feb 21, 2015 at 23:24
  • @JamesKanze Sorry, but if I delete copy constructor, neither of the two will work. If I keep a copy constructor and delete assignment operator, both direct initialisation and copy initialisation work.
    – Christophe
    Feb 21, 2015 at 23:55
1

This one does not use the assignment operator (the two lines are equivalent, as far as I know). = is used in the syntax but operator= is not actually used:

myClass myVariable = myClass(123);
//or
myClass myVariable(123);

This one uses the assignment operator:

myClass myVariable;
myVariable = myClass(123);

If assignment operator is badly or not implemented, first statement works, second may (and will most likely) crash.

#include <iostream>
#include <string.h>
using namespace std;

class Dvector
{
public:
    Dvector( int thesize = 0 )
    {
        std::cout << "Constructing object of size " << thesize << std::endl;
        size = thesize;
        data = new double[size];
    }
    Dvector( const Dvector& v )
    {
        std::cout << "Constructing object of size " << v.size << " by copy" << std::endl;
        size = v.size;
        data = new double[size];
        memcpy( data, v.data, sizeof(double)*size );
    }
    Dvector& operator=( const Dvector& v )
    {
        std::cout << "Assigning object of size " << v.size << std::endl;
        if ( &v != this )
        {
            size = v.size;
            data = new double[size];
            memcpy( data, v.data, sizeof(double)*size );
        }
        return *this;
    }
    ~Dvector()
    {
        std::cout << "Destroying object" << std::endl;
        delete [] data;
    }
private:
    double* data;
    int size;
};

int main() {
    Dvector v = Dvector(3);
    return 0;
}

Displays:

Constructing object of size 3 
Destroying object

When:

int main() {
    Dvector v;
    v = Dvector(3);
    return 0;
}

Displays:

Constructing object of size 0
Constructing object of size 3
Assigning object of size 3
Destroying object
Destroying object

And would have crashed if copy constructor was not defined...because then v.data ends up pointing to data allocated by temporary variables (Dvector(3)) and then deleted. Possible crash when trying to access v.data or upon v destruction (deleting already freed memory).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.