AClass ac=AClass("name",23); VS. AClass ac("name", 23);

  • Thread starter Alfred E Neuman
  • Start date
A

Alfred E Neuman

I've read that... AClass ac = AClass( "name", 23 );

is less efficient than... AClass ac( "name", 23 );

because the first has to create a temporary object which is then
assigned to ac.

However, I can't seem to prove this true. I've created a class
containing the following constructors and overloaded equal operator.

/* default constructor */ AClass();
/* main constructor */ AClass( const char *, int );
/* copy constructor */ AClass( const AClass & );
/* overloaded = operator */ AClass operator=( const AClass & );

I placed cout statements in each to show program execution flow.

AClass ac = AClass( "name", 23 ); executes my main constructor once
and a matching destructor call once. I never see any other
constructor or destructor calls.

However,

AClass ac;
ac = AClass( "name", 23 );

obviously calls two constructors; the default for object ac and then
the main constructor for the temporary object which is then assigned
to ac. Matching destructor calls are made as well. This makes sense
because of the two statement setup.

My confusion is that I see no difference between

AClass ac = AClass( "name", 23 ); and
AClass ac( "name", 23 );

What am I missing here? Was the information I read wrong? Is there
some kind of compiler optimization going on? I've tried this under
Microsoft Visual C++, Borland C++, and gcc on Linux, all with the same
results. I'm stumped.
 
R

Ron Natalie

Alfred E Neuman said:
I've read that... AClass ac = AClass( "name", 23 );

is less efficient than... AClass ac( "name", 23 );

because the first has to create a temporary object which is then
assigned to ac.

There is NEVER any assignment in either of the cases. What there
is logically in the first case is copy construction from the temporary.
However, no compiler I've ever come accross fails to optimize away
the temporary. In both cases ac is built directly from the two args
you provided.
 
M

Michael Kochetkov

Here goes several extra words to Ron's reply.

[...]
My confusion is that I see no difference between

AClass ac = AClass( "name", 23 ); and
AClass ac( "name", 23 );
The first case requires AClass to have the properly declared copy
constructor. Properly declared in this case means that it shall be
accessible in the context and its, constructor's, argument shall have const
(or const volatile) AClass& form. The second is because of the temporary
been involved in. As you were already told it is not necessary that the copy
constructor would be called in this case.
The second case does not require an AClass copy constructor declaration to
compile and link.
 
A

Alfred E Neuman

Ok, so the answer boils down to this being a common compiler
optimization implemented into virtually all C++ compilers.

Thanks for clearing this up!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top