4

What is it? I have a vague understanding that a synthesized constructor is a constructor implicitly created by the compiler which also initializes its member class objects, but may or may not fulfilling the needs of the program implementation. Is this definition correct?

And in what cases will it be synthesized, and it what cases, not?

Amumu
  • 17,924
  • 31
  • 84
  • 131
  • possible duplicate of [When does the compiler provide definitions for the special members of a class?](http://stackoverflow.com/questions/8621052/when-does-the-compiler-provide-definitions-for-the-special-members-of-a-class) – Xeo Feb 17 '12 at 04:12
  • http://stackoverflow.com/q/1810163/14065 – Martin York Feb 17 '12 at 04:47

1 Answers1

4

The following article answers your question better than I ever could. I have quoted a brief passage from the article to give you an idea of its flavor. A link appears below the quote.

Incidentally, the "C++ Reference Guide" from which I am quoting claims to have 529 pages of nutritious C++ information in it; you might want to bookmark it.

A constructor initializes an object. A default constructor is one that can be invoked without any arguments. If there is no user-declared constructor for a class, and if the class doesn't contain const or reference data members, C++ implicitly declares a default constructor for it.

Such an implicitly declared default constructor performs the initialization operations needed to create an object of this type. Note, however, that these operations don't involve initialization of user-declared data members.

For example:

class C
{
    private:
        int n;
        char *p;
    public:
        virtual ~C() {}
};

void f()
{
    C obj; // 1 implicitly-defined constructor is invoked
}

C++ synthesized a constructor for class C because it contains a virtual member function. Upon construction, C++ initializes a hidden data member called the virtual pointer, which every polymorphic class has. This pointer holds the address of a dispatch table that contains all the virtual member functions' addresses for that class.

The synthesized constructor doesn't initialize the data members n and p, nor does it allocate memory for the data pointed to by the latter. These data members have an indeterminate value once obj has been constructed. This is because the synthesized default constructor performs only the initialization operations that are required by the implementation—not the programmer—to construct an object.

http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=15

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 1
    So, what's the meaning of "synthesized" here? What does the constructor synthesized with what? I was wondering the usage of the word "synthesized", to understand what it explains in the constructor context. – Amumu Feb 17 '12 at 05:02
  • 1
    @Anumu: A synthesized constructor is a constructor that *you didn't write.* In C#, we call it the "default" constructor. – Robert Harvey Feb 17 '12 at 18:20