views:

314

answers:

4

Hi,

I wanted to What is the Job of Compiler Supplied Constructor ?. Is that constructor does memory allocation and all the stuffs required to create an object.

I am not asking this question from member variable initialization point of view. I want to know what happens inside the code of default constructor that compiler executes and supplies default constructor

+5  A: 

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.

If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A(). This constructor is an inline public member of its class. The compiler will implicitly define A::A() when the compiler uses this constructor to create an object of type A. The constructor will have no constructor initializer and a null body.

The compiler first implicitly defines the implicitly declared constructors of the base classes and nonstatic data members of a class A before defining the implicitly declared constructor of A. No default constructor is created for a class that has any constant or reference type members.

A constructor of a class A is trivial if all the following are true:

* It is implicitly defined
* A has no virtual functions and no virtual base classes
* All the direct base classes of A have trivial constructors
* The classes of all the nonstatic data members of A have trivial constructors

If any of the above are false, then the constructor is nontrivial.

A union member cannot be of a class type that has a nontrivial constructor.

Like all functions, a constructor can have default arguments. They are used to initialize member objects. If default values are supplied, the trailing arguments can be omitted in the expression list of the constructor. Note that if a constructor has any arguments that do not have default values, it is not a default constructor.

Read

Default constructors (C++ only)

rahul
phoenix, thanks for answer some of the above are new to me :)...but i wanted to know during construction of object..what are the stuffs that compiler does before complete object is created. your answer is after construction of object.
mahesh
Though IBM references are usually good that is not a good definition:
Martin York
<quote>This constructor is an inline public member of its class</quote> Even if that is true (and I would need to have a quote from the standard to convince me) its not really relevant.
Martin York
<quote>The constructor will have no constructor initializer</quote>Assuming you are talking about the initializer list (otherwise I apologize) this is blatantly wrong (unless you are talking about a class with no base class and no members).
Martin York
+1  A: 

what are the stuffs that compiler does before complete object is created

The compiler-supplied default constructor will do the following:

  • Invoke the default constructor of the base class (if there is a base class)
  • Invoke the default constructor for each of the class's data members (for each data member that has a constructor at all, which means not primitive types) ... and if an exception is thrown by one of these, then invoke the destructor for any already-previously-created data members.

"a constructor is not quite an ordinary function. In particular, it interacts with memory management routines in ways ordinary member functions don’t." : This type of answer i am looking..if anybody know more..please let me know ? :)

If you do ...

Something* something = new Something();

... then that invokes "operator new" (and the default operator new will allocate memory from the heap), and then invokes the constructor on the newly-allocated memory. Alteratively if you do:

Something something;

... then that allocates (or reserves) some memory on the stack, and then invokes the constructor on the newly-allocated/reserved memory.

It's similar to every other non-static method (including the destructor) in the fact that there's a "this" pointer: the difference is that when the constructor is called, the memory which is going to contain "this" has just been allocated but hasn't been initialized yet (which is what the constructor does: it initializes "this").

ChrisW
ChrisW, Good answer...i wanted to know what happens inside the code of constructor that produces default constructor
mahesh
I don't know whether I've answered/understood your question.
ChrisW
+2  A: 

A default constructor.

It has zero parameters (or all its parameters have defaults) so that an object can be constructed without any parameters.

If you do not define any constructors the compiler will generate a default constructor for you.
The compiler generated default constructor does (in the following order)

  • Call the default constructor of all base classes (if any, but in order).
  • Call the default constructor of each member (in the order they are declared in the class).
    • If the member is a int/float/char etc it is undefined.
    • If the member is a pointer it is undefined.

The copy constructor:

If you do not define a copy constructor then the compiler will generate a default copy constructor.
The compiler generate copy constructor does (in the following order)

  • Call the copy constructor of all the base classes (if any, but in order).
  • Call the copy constructor of each member (in the order they are declared in the class).
    • Each member is passed the corresponding member of the object being copied.
    • Note for pointers this means just copy the pointer value.
      This is why we get the shallow copy problem when the class contains a RAW pointer that the object manages.

Though not a constructor. It is important to note that the compiler will also auto generate the assignment operator when one is not defined.
The compiler generated assignment operator does (in the following order)

  • Call the assignment operator of each base class (if any, but in order).
  • Call the assignment operator of each member (in the order they are declared in the class).
    • Each member is passed the corresponding member of the object being copied.
    • Note for pointers this means just copy the pointer value.
      This is why we get the shallow copy problem when the class contains a RAW pointer that the object manages.

The default Destructor:
Most people think the destructor is trivial or not there. But it is important to note what the default version is actually doing (it is slightly more than nothing).

  • Each member has its destructor called (in reverse order of declaration)
  • Note int/float/pointers do not have destructors so nothing explicit is done.
  • Each base class destructor is called in reverse order.

If you define a destructor the behavior does not change. But the code you define as part of the destructor is executed before the behavior defined above. So in affect there is always a destructor just the code is an empty block.

Note: There are some special considerations when virtual base classes are used. But that's another question.

So even if you define an empty class. The compiler will always generate four methods for you:

class X: public Z
{
    int a;
    Y   b;
    Z*  c;
};

// Compiler generated methods will look like this:
X::X()
  :Z() // Construct base class.
  //,a?? The default construction of an int does nothing the value is undefined.
  ,b()
  //,c?? The default construction of a pointer does nothing,
{}
X::~X()
{} // Note members are destoyed here
   // ~c:  Does nothing it is a pointer.
   // ~b:  destroyes b via Y::~Y()
   // ~a:  Does nothing as POD has not destructr.
   // ~Z(): Destory base class.
X::(X const& rhs)
    :Z(rhs)
    ,a(rhs.a)
    ,b(rhs.b)
    ,c(rhs.c)
{}
X& operator=(X const& rhs)
{
    Z::operator=(rhs);
    a = rhs.a;
    b = rhs.b;
    c = rhs.c;
    return *this;
}
Martin York
A: 

Here is what i got from Stroustrup Book

"a constructor is not quite an ordinary function. In particular, it interacts with memory management routines in ways ordinary member functions don’t." : This type of answer i am looking..if anybody know more..please let me know ? :)

mahesh
I added to my answer to try to answer that.
ChrisW
Martin York and ChrisW both answers look impressive. +1 for both. But i can't accept both answer right which i am intended to do
mahesh