2

Hey guys i have this code Animal.h:

    /*
 * Animal.h
 *
 *  Created on: May 27, 2015
 *      Author: saif
 */

#ifndef ANIMAL_H_
#define ANIMAL_H_
#include <iostream>
using namespace std;
class Animal {
    int age;
public:
    Animal();
    Animal(const Animal & rhs);
    Animal addTo();
    virtual ~Animal();
};

#endif /* ANIMAL_H_ */

Animal.cpp:

/*
 * Animal.cpp
 *
 *  Created on: May 27, 2015
 *      Author: saif
 */

#include "Animal.h"

Animal::Animal()
:age(0)
{

}

Animal::Animal(const Animal & rhs)
:age(rhs.age)
{
    cout << "copy constructor activated" << endl;
}
Animal Animal::addTo()
{
    Animal ret;
    ret.age = 5;
    return ret;
}
Animal::~Animal() {
    cout << "NO not done, destructing.."<< endl;
}

main.cpp

#include <iostream>
using namespace std;
#include "Animal.h"
int main() {
    Animal a;
    Animal b = a.addTo();
    cout << "done" << endl;
    return 0;
}

Output:

done
NO not done, destructing..
NO not done, destructing..

Expected output:

    copy constructor activated
    done
    NO not done, destructing..
    NO not done, destructing.

Or even:

    copy constructor activated
    copy constructor activated
    done
    NO not done, destructing..
    NO not done, destructing.

Because I read from books and other resources that when we return function by value or give parameters to function by value it calls copy constructor because it makes a temporary copy, and also I have in main a situation (line 6) where copy constructor must be activated, but it didn't.. why?

MaxDevelop
  • 637
  • 1
  • 4
  • 16

1 Answers1

1

According to the C++ Standard (12.8 Copying and moving class objects)

31 When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.124 This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

— in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cvunqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

— 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

Take into account that in any case the copy constructor shall be available. For example if you will declare the copy constructor as private then the compiler will issue an error (if it is not a MS VS compiler:) )

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335