Sign in to follow this  
aeroz

C++ Class Initialization

Recommended Posts

kkrz    144
In the first case, a temporary object of the class type may be created and then the copy constructor invoked. However, the object may also be constructed directly from the value.

Also consider the following:

[CODE]
struct a{ a() {} };

struct b{ b(a) {} };

struct c{ c(b) {} };

int main()
{
a objA;
c x( objA ); // this compiles
c y = objA; // this does NOT compile, but it would if "a" defined an "operator c();"
}
[/CODE]

Share this post


Link to post
Share on other sites
aeroz    171
Thanks for the explanation.

But when I have the same class on both sides, does it still make a difference?

MyClass c1;
MyClass c2 = c1;
// vs
MyClass c2 ( c1 );
// obsolutely the same?

It just seems a bit strange to me that compilers can choose the behavior of a program. (What if I want to rely on the destructor being called? It is not called for the temporary object when the compiler optimizes the program. Well, relying on this is probably bad style.)

I usually use the () syntax. But = is more "natural", I think. (at least when you have the same object type on both sides)

Example in boost:
boost::shared_ptr<std::string> x = boost::make_shared<std::string>("hello, world!");

I always see make_shared being used like this.

But I write always

boost::shared_ptr<std::string> x ( boost::make_shared<std::string>("hello, world!") );

but it is less readable. I wanted to know if I can safely change it to the = method. (with certitude that there can be no overhead)

[Edited by - aeroz on November 13, 2010 8:50:05 AM]

Share this post


Link to post
Share on other sites
Xycaleth    2391
I'm fairly sure that both methods are the same. They both call the constructor for the class being initialised, but they are just two ways of writing the same thing. The same applies to in-built types too.
int i (5);

and
int i = 5;

are equivalent.

Share this post


Link to post
Share on other sites
kkrz    144
Also keep in mind if the constructor is explicit, you may only create the object via direct initialization. Look up the keyword explicit and you will see some examples.

Quote:

MyClass c1;
MyClass c2 = c1;
// vs
MyClass c2 ( c1 );
// obsolutely the same?

It just seems a bit strange to me that compilers can choose the behavior of a program.


I believe in this case, the behavior will be identical. Here is the relevant part of the standard:

If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered. The applicable constructors are enumerated (13.3.1.3), and the best one is chosen through overload resolution (13.3). The constructor so selected is called to initialize the object, with the initializer expression(s) as its argument(s). If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed.

— Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type. The temporary is a prvalue. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized; see 12.2, 12.8.

Quote:

It just seems a bit strange to me that compilers can choose the behavior of a program.


Realistically, I believe the compiler will optimize out any temporary object creations and create the object directly from the value if it is at all possible. Maybe someone more experienced could weigh in and confirm/deny.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this