I tried the following source code:
Code:
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass() { cout <<"----------MyClass Constructor..."<<endl;} // constructor
MyClass(MyClass&) { cout <<"+++++++MyClass Copy Constructor..." << endl;} // copy constructor
~MyClass() { cout <<"=======MyClass Destructor..." << endl;} // destructor
};
MyClass Function1 ()
{
cout << endl << endl << "Ok, boss. This is inside the function1" << endl;
cout << "Nothing is returned" << endl;
}
int main()
{
cout << "I am going to create MyClass MyObject" << endl;
MyClass MyObject;
cout << "MyObject has just created" << endl << endl;
cout << endl << "I am going to call the MyClass Function1 into the next line." << endl;
Function1();
cout << endl << endl << endl << "We are back to main." << endl;
cout << "Zero is going to returned and exit main.." << endl;
return 0;
}
The Output is:
Code:
I am going to create MyClass MyObject
----------MyClass Constructor...
MyClass MyObject has just created
I am going to call the MyClass Function1 into the next line.
Ok, boss. This is inside the function1
Nothing is returned
=======MyClass Destructor...
We are back to main.
Zero is going to returned and exit main..
=======MyClass Destructor...
Analysis:
When I create a object, the constructor of the class is called.
When I call a object-oriented function without any arguments, none constructor is called.
When I exit from the this function, without returning a value or something, the destructor of the class is called.
When I eventually exit from int main() function, the destructor of the class is called once again.
So,
1) when you create an object, the constructor is called.
2) when you exit from any type (int, char, class, etc) of function, the destructor is called.Even if you haven't create an object in order to -in some way- activate the object oriented engine, the destructor is called.
So let's go further to my source code example number two.
Code:
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass() { cout <<"----------MyClass Constructor..."<<endl;} // constructor
MyClass(MyClass&) { cout <<"+++++++MyClass Copy Constructor..." << endl;} // copy constructor
~MyClass() { cout <<"=======MyClass Destructor..." << endl;} // destructor
};
MyClass Function1 (MyClass MyObject)
{
cout << endl << endl << "Ok, boss. This is inside the function1" << endl;
cout << "Nothing is returned" << endl;
}
int main()
{
cout << "I am going to create MyClass MyObject" << endl;
MyClass MyObject;
cout << "MyObject has just created" << endl << endl;
cout << endl << "I am going to call the MyClass Function1 into the next line with an Object as an arguement." << endl;
Function1(MyObject);
cout << endl << endl << "We are back to main." << endl;
cout << "Zero is going to returned and exit main.." << endl;
return 0;
}
Output
Code:
I am going to create MyClass MyObject
----------MyClass Constructor...
MyClass MyObject has just created
I am going to call the MyClass Function1 into the next line with an Object as an arguement.
+++++++MyClass Copy Constructor...
Ok, boss. This is inside the function1
Nothing is returned
=======MyClass Destructor...
=======MyClass Destructor...
We are back to main.
Zero is going to returned and exit main..
=======MyClass Destructor...
Analysis:
When you call a class function with a class object as an argument, then the Copy Constructor is called. This is becauce
(by calling this function) there is another temporary copy of this class object into the stack.
So the copy constructor is called each time a temporary copy of the object is put on the stack.
Then when, the function exits, this temporary copy of the objects is destroyed, so the object's destructor is called.
Then the function exits, and the destructor is recalled.
Eventually the main() exits too, and the destructor is called for once last time.
No, let's delve it :P
Source Code Example Number 3
Code:
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass() { cout <<"----------MyClass Constructor..."<<endl;} // constructor
MyClass(MyClass&) { cout <<"+++++++MyClass Copy Constructor..." << endl;} // copy constructor
~MyClass() { cout <<"=======MyClass Destructor..." << endl;} // destructor
};
MyClass Function1 (MyClass MyObject)
{
cout << endl << endl << "Ok, boss. This is inside the function1" << endl;
cout << "The copy of the Object is returned" << endl;
return MyObject;
}
int main()
{
cout << "I am going to create MyClass MyObject" << endl;
MyClass MyObject;
cout << "MyObject has just created" << endl << endl;
cout << endl << "I am going to call the MyClass Function1 into the next line." << endl;
Function1(MyObject);
cout << endl << endl << "We are back to main." << endl;
cout << "Zero is going to returned and exit main.." << endl;
return 0;
}
Output:
Code:
I am going to create MyClass MyObject
----------MyClass Constructor...
MyClass MyObject has just created
I am going to call the MyClass Function1 into the next line with an Object as an arguement.
+++++++MyClass Copy Constructor...
Ok, boss. This is inside the function1
The copy of the Object is returned
+++++++MyClass Copy Constructor...
=======MyClass Destructor...
=======MyClass Destructor...
We are back to main.
Zero is going to returned and exit main..
=======MyClass Destructor...
Analysis:
The output here is the same as the above, but...there are some details. This time, the function returns the object.
To make this happen, a copy of that object is re-made (the second) is the stack, so the copy constructor is called.
Am I right guys ?