C++ What is the use of a implict default constructor provided by the comiler ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carbon
    New Member
    • Mar 2014
    • 9

    C++ What is the use of a implict default constructor provided by the comiler ?

    In C++ if no user defined constructor is provided for a class the compiler automatically provides a implicit default constructor.

    An implicit default constructor is equivalent to a default constructor with no parameters and no body ?

    So what is the use of a implicit default constructor provided by the compiler, if it is not going to do anything ?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The default constructor calls the constructor for each member of your class. So it does something. Like here:

    Code:
    MyClass obj;
    obj will have each member initialized by calling its constructor.

    Comment

    • coderHead
      New Member
      • Feb 2012
      • 6

      #3
      You say each member is initialized by calling its constructor. OK, initialized to what? To whatever the constructor feels like? LOL. I mean if no user defined constructor is provided then where are the initializing values coming from?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        The compiler knows how to initialize the built-in types but if your class has user-defined type as a member and there is no default constructor for that type, then your compile fails on this code:

        Code:
        MyClass obj;
        Initialization is a big deal in C++ since major initialization errors in C slip by the C compiler. One of the C++ goals was to plug these errors at compile time rather than when the program was running.

        C does not do member-wise initialization. It doesn't do any initialization at all. There is reliance on the mem--- functions which can easily screw up the stack.

        Comment

        Working...