0
class fruit{
  public:
        fruit(const fruit &f){ 
          cout << "copy constructor invoked ";
        }
        ~fruit(){
          cout << "destructor invoked ";
        }
        fruit(){
         cout << "constructor invoked ";
       }
}  

int main(){
   fruit f1; // o/p constructor invoked
   fruit f = fruit(); // o/p constructor invoked 
   fruit(); // o/p constructor invoked and destructor invoked
   fruit f2 = fruit(fruit(fruit(f1))); // can anyone explain this statement?
              // o/p copy constructor invoked 
   return 0; // o/p destructor invoked destuctor invoked
}

I have learnt that fruit f = fruit () the fruit() creates temporary object and assigns it to f using copy constructor. My questions are

  1. If fruit() creates temporary object then destructor must be invoked twice but destructor is invoked only once i.e for f
  2. If it assigns temporary object to f using copy constructor, here it does not invoke copy constructor at all.? (does compiler generate another copy constructor internally for copying temporary objects ?)

If the concept is about creating temporary objects then statement fruit f2 = fruit(fruit(fruit(f1))) does not invoke any constructors and destructors, it invokes only copy constructor.

How does statement really work when X x = X(); I understand how it works if it is X x(x).

5
  • 3
    The copy usually gets elided and the object created in place. See copy elision. Nov 7, 2014 at 7:31
  • There is no fruit(2) in your code (and even if there was, it wouldn't compile)!!! Nov 7, 2014 at 7:41
  • @barakmanos sorry it was just a typo mistake. Nov 7, 2014 at 7:42
  • T var = k is equivalent to T var(k). Nov 7, 2014 at 7:44
  • 2
    @barakmanos No, it isn't. struct S { S(int) { } S(const S &) = delete; }; int main() { S s(3); } works. S s = 3; does not.
    – user743382
    Nov 7, 2014 at 7:47

1 Answer 1

-1
fruit f2 = fruit(fruit(fruit(f1))); 

I think this one is optimized by your compiler to fruit f2 = fruit(f1); because the compiler notices that you make a copy of a (temporary) copy of a (temporary)copy and that that in the result is the same as just making one copy.

Edit: deleted the statement which was discussed in the comments

6
  • 1
    They are not the same. Copy elision is only a possibility, and the former form cannot compile if the copy constructor is not accessible.
    – Siyuan Ren
    Nov 7, 2014 at 8:30
  • I tried it without the copy constructor and fruit f = fruit(); still compiled.
    – klausi
    Nov 7, 2014 at 8:56
  • Two possibilities: the compiler creates one for you because you do not explicitly delete it; or you are using MSVC, which is non-compliant with respect to this.
    – Siyuan Ren
    Nov 7, 2014 at 8:57
  • Yes, my mistake if you declare it private it does not compile. But is it right to say >in this case< it is the same?
    – klausi
    Nov 7, 2014 at 9:08
  • No. As I said, copy elision is not mandated by the standards, so it depends on the compiler and optimization flags. If this kind of the compiler dependent relationship qualifies as being "the same", then you might as well say "int32_t" is the same as "int".
    – Siyuan Ren
    Nov 7, 2014 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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