0

Hope my thread finds you well.

I've tried something and I need an explanation

I've made a class for complex numbers, printing the word "constructor" in the constructor implementation and the word "destructor" in the destructor implementation.

I've declared 3 objects, sent them by value to a Non-class-member function that takes two objects from the class as arguments and return a class object as the summation of two complex numbers

what I've found is that, the number of destructor calls is not equal to the number of constructor calls, actually they are more .

I've got 2 constructor calls and 4 destructor calls.

Does any one have any explanation for this thing?

Many thanks for your cooperation and help.

user2268349
  • 33
  • 1
  • 5
  • 1
    Define also the copy constructor and print its evocation as well. Probably you'll level the number of constructions and destructions this way. – 101010 Nov 06 '15 at 23:41
  • Are you printing your logs in all constructors (default constructor, copy constructor and move constructor) or only one of them? – Paul Nov 06 '15 at 23:41
  • I've implemented the default constructor and a constructor that takes 2 arguments, I've used only both of them, and placed the print instruction in both of them as well. – user2268349 Nov 06 '15 at 23:43
  • @101010 : Even if I didn't use them ? – user2268349 Nov 06 '15 at 23:44
  • You're probably using them implicitly. Read about the [rule of five](http://en.cppreference.com/w/cpp/language/rule_of_three). – 101010 Nov 06 '15 at 23:46
  • I've sent them as arguments, returned them as well, but before all of that, I've declared them by the default constructor, am I missing something ? – user2268349 Nov 06 '15 at 23:50

1 Answers1

0

There are many constructors and some of them may be created for you:

Default constructor MyClass() does the minimum possible work to set up an object of MyClass.

Copy constructor MyClass(const MyClass & source) copies source into a brand new object of MyClass

Move constructor MyClass(MyClass && source) moves the contents of source into a brand new object of MyClass and leaves source empty, but in a safe state.

A lot of the time you'll find the copy constructor being used quietly in the background without you declaring one or asking for it. For example,

myVector.push_back(myObject); 

Will often copy myObject into myVector.

void MyFunction(MyClass myObject)

Accepts myObject by value and may copy the source object in the process. To see this in action:

void MyFunction(MyClass myObject) // copy constructs myObject from the caller's parameter
{
    myVector.push_back(myObject); // copy constructs a myClass into myVector from myObject
} // destructs myObject

In this call two new objects were created and one destroyed. If there you have not defined a copy constructor with a print line so show it happened, you will only see a mysterious call of the destructor. When the vector is destroyed, cleared, or myObject is removed from it, there will be another destructor call.

MyClass MyOtherFunction()

Will copy the returned MyClass if it has to, but usually finds a way around it.

This is not a solution, but may be a path to a solution: Add a copy constructor with a print statement in it.

MyClass(const MyClass & source)
{
    // copy all member variables from source to this
    std::cout << "Copy constructor called" << std::endl;
}

And I recommend that you read up on the Rule of Three to find out why this is important. Hint: It has nothing to do with over-achieving Sith.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54