There are many ways to initialize a variable in C++. Some of them are quite obscure. When you write a piece of code from scratch, you will usually know what to do, but a problem may occur when you're pondering over a piece of code written by someone else, who is creative in his use of various initialization techniques. In the following code snippet, I present some methods of variable initialization, along with heavy commenting to make the subject perfectly clear. The example also includes some test code to show the difference between various initializations obvious.
#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;
}

Comments

comments powered by Disqus