4

Possible Duplicate:
constructor invocation mechanism

Suppose we have a class ABC then:

1) Is the following initialization possible? If yes the what are the detailed steps:

 ABC a( ABC() );

2) What's the difference(performance etc.) between these two forms of object creation?

ABC a;
ABC b=ABC();
Community
  • 1
  • 1
rsjethani
  • 2,179
  • 6
  • 24
  • 30
  • 5
    `ABC a( ABC() );` declares a function and not an object. – Prasoon Saurav Feb 07 '12 at 06:23
  • I don't understand why the question is down voted...I asked them as a part of my research/quest of c++. :( – rsjethani Feb 07 '12 at 06:30
  • @Prasoon: No, there are two objects here. An unnamed temporary instance of ABC which is passed as the argument for the copy constructor to initialize a. – Axel Feb 07 '12 at 06:31
  • @Prasoon:ABC a(ABC); declares a function...I think you are wrong otherwise please elaborate.thanks – rsjethani Feb 07 '12 at 06:32
  • 2
    @Axel : http://ideone.com/As4F7. – Prasoon Saurav Feb 07 '12 at 06:34
  • 1
    @ryanlancer : Check out this answer: http://stackoverflow.com/questions/4283576/constructor-invocation-mechanism/4283589#4283589 – Prasoon Saurav Feb 07 '12 at 06:35
  • @PrasoonSaurav, interesting. Meaning wise, is this syntax same as: `ABC a(ABC);` ? – iammilind Feb 07 '12 at 06:38
  • @iammilind : `ABC a(ABC)` declares a function that takes an object of ABC as argument whereas `ABC a(ABC())` declares a function that takes an object of type pointer to function returning an object of ABC and taking no argument. This is also known as most vexing parse in C++. – Prasoon Saurav Feb 07 '12 at 06:41

3 Answers3

1
 ABC a( ABC() );

Declares an function by the name a which takes an object of type pointer to function returning an object of ABC and taking no arguments. There is no object creation here.

ABC a;

Creates an object of the type ABC by calling the default constructor.

ABC b=ABC();

Creates an temporary object of type ABC and then calls the copy constructor to copy this object in to b.
Important to note here that the compiler might omit the copy constructor call here by applying return value optimization.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Hi. Instead of giving a repeated answer you can close this question as a duplicate of http://stackoverflow.com/questions/4283576/constructor-invocation-mechanism/ – Prasoon Saurav Feb 07 '12 at 07:08
  • @PrasoonSaurav: There was no duplicate marked when I answered this.Now that I see one being marked, I will check it out and vote so If I feel it indeed is an dup. – Alok Save Feb 07 '12 at 07:10
  • @Als:I disabled optimizations in VS 2010 but still default constructor is the only one being called??? – rsjethani Feb 07 '12 at 07:16
  • @ryanlancer: [Return Value Optimization](http://en.wikipedia.org/wiki/Return_value_optimization) is the one optimization that the compiler is allowed to do as and when it can, even if there are side effects involved.This is explicitly allowed by the standard.In Short,the compiler is allowed to & will optimize it even without optimizations disabled. GCC allows an explicit setting `--no-elide-constructors` to disable RVO.You can with that option and You will notice the behavior you expect. – Alok Save Feb 07 '12 at 07:18
1

1) As written, that's a function declaration; you need to change it slightly to get the behaviour you intend:

ABC a((ABC()));

or

ABC a = ABC();

In this case, the compiler has two choices:

  • Create a temporary object using the default constructor, and initialise a from that using the copy constructor;
  • Elide the copy, and directly create a using the default constructor.

In both cases, there must be an accessible copy constructor, even if it is not actually used.

2)

ABC a;

That will create a using the default constructor.

ABC b = ABC();

That will do the same as in question 1; it's up to the compiler whether to elide the copy.

So the first form could potentially be more efficient, if the compiler doesn't perform that particular optimisation, and if copying a default-initialised object is expensive.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
-2

1) firstly creates an instance of ABC (the inner one, constructor is called without any parameter) and passes this (temporary!!!) instance to the copy constructor of ABC. This is absolutely useless if the copy-constructor does what it's supposed to.

2) declares a variable having the name a and in the same time instanciates it using a constructor without arguments (default constructor). Both versions ins 2) are absolutely equal.

Atmocreations
  • 9,923
  • 15
  • 67
  • 102