#include <iostream>
using namespace std;
class Foo
{
public:
// Default constructor
//
Foo()
{
cout << "Default c'tor was called!\n";
}
// Copy constructor
//
Foo(const Foo&)
{
cout << "Copy c'tor was called!\n";
}
// Assignment operator
//
Foo& operator=(const Foo&)
{
cout << "Assignmnent operator was called!\n";
}
};
int main()
{
// #1
// Just a declaration. f1 will be initialized
// with whatever the default c'tor was
// designed to do
//
cout << "Trying init method #1: ";
Foo f1;
// #2
// Direct initialization. The copy c'tor
// will be called to initialize f2 with f1
//
cout << "Trying init method #2: ";
Foo f2(f1);
// #3
// Although the '=' sign is used, this is the
// same as before, f3 is initialized with f1
// by the copy c'tor (note, the assignment
// operator isn't invoked)
//
cout << "Trying init method #3: ";
Foo f3 = f1;
// #4
// Does it look like a declaration? It sure
// does... and it is a declaration allright,
// but not of Foo object! This is tricky...
// What is declared is a function called f4,
// which takes no parameters and returns
// a Foo
//
cout << "Trying init method #4: ";
Foo f4();
return 0;
}
Variable initialization in C++
For comments, please send me an email.