You can test how it works with this code...
Code:
#include <iostream>
using namespace std;
class TestClass {
public:
TestClass( int testValue = 0 ){
classID = ++count;
cout << "Constructor: " << classID << " called" << endl;
cout << "Value passed: " << testValue << endl;
}
~TestClass() {
cout << "Destructor: " << classID << " called" << endl;
}
private:
int testValue;
int classID;
static int count;
};
int TestClass::count = 0;
int main()
{
TestClass Class1;
TestClass Class2(2);
TestClass Class3 = TestClass(5);
return 0;
}
Here's the output.
Code:
EXECUTING:
/home/rjones/projects/testClass
----------------------------------------------
Constructor: 1 called
Value passed: 0
Constructor: 2 called
Value passed: 2
Constructor: 3 called
Value passed: 5
Destructor: 3 called
Destructor: 2 called
Destructor: 1 called
----------------------------------------------
Program exited successfully with errcode (0)
Press the Enter key to close this terminal ...
As you'll see from the output, when TestClass Class3 = TestClass(5); is called, only one constructor is called. If the = were used, then a temporary obj would have been created then copied.