Reputation: 869
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?
Upvotes: 6
Views: 291
Reputation: 21514
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).
Upvotes: 1
Reputation:
myClass myVariable = myClass(123);
myClass myVariable(123);
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
Upvotes: 10
Reputation:
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.
Upvotes: 1