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.