Default constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • al

    Default constructor

    class Base
    {
    public:
    //Base() {}
    Base(int m) {}

    };

    class Derive : public Base
    {
    public:
    Derive(int m) {}

    };


    The above code keeps giving compiler error: 'Base': no appropriate default
    constructor available, until a default cstor, Base() is added into Base
    class.

    Does this mean that a default cstor has always to be added whenever a
    non-default cstor present?

    Or is this default cstor, Base(), only required during inheritance? How to
    make Derive to call Base(int m) rather than Base()?

    In general, when does a default cstor have to be provided?

    Thanks!


  • Adam Fineman

    #2
    Re: Default constructor

    al wrote:[color=blue]
    > class Base
    > {
    > public:
    > //Base() {}
    > Base(int m) {}
    >
    > };
    >
    > class Derive : public Base
    > {
    > public:
    > Derive(int m) {}
    >
    > };
    >
    >
    > The above code keeps giving compiler error: 'Base': no appropriate default
    > constructor available, until a default cstor, Base() is added into Base
    > class.[/color]

    That's because your Derive contructor is attempting to construct a
    default Base object, since you haven't told it how to initialize it's
    Base component. Change your Derive contructor to this:

    Derive(int m)
    : Baee(m)
    {
    /* no code */
    }

    and it will compile without a default contructor for Base.
    <snip>[color=blue]
    > In general, when does a default cstor have to be provided?
    >[/color]
    Whenever an object must be initialized without any parameters.

    - Adam

    --
    Reverse domain name to reply.

    Comment

    • Victor Bazarov

      #3
      Re: Default constructor

      "al" <allin@168.ne t> wrote...[color=blue]
      > class Base
      > {
      > public:
      > //Base() {}
      > Base(int m) {}
      >
      > };
      >
      > class Derive : public Base
      > {
      > public:
      > Derive(int m) {}
      >
      > };
      >
      >
      > The above code keeps giving compiler error: 'Base': no appropriate default
      > constructor available, until a default cstor, Base() is added into Base
      > class.[/color]

      Correct. In the 'Derive's constructor you don't initialise the Base in any
      way. The compiler has to do it itself. It uses default-initialisation.
      Since you have a parameterised c-tor in Base, and no default c-tor, the
      compiler cannot initialise the Base part of Derive.
      [color=blue]
      >
      > Does this mean that a default cstor has always to be added whenever a
      > non-default cstor present?[/color]

      No.
      [color=blue]
      >
      > Or is this default cstor, Base(), only required during inheritance? How[/color]
      to[color=blue]
      > make Derive to call Base(int m) rather than Base()?[/color]

      class Derive : public Base
      {
      public:
      Derive(int m) : Base(m) {}
      };

      BTW, what book are you using to learn C++ that doesn't tell you that?
      [color=blue]
      >
      > In general, when does a default cstor have to be provided?[/color]

      No.

      Victor


      Comment

      • Dan Cernat

        #4
        Re: Default constructor

        In addition:

        if you provide no constructor, the compiler will generate a 'default' (no
        parameters) constructor. As soon as you declare a constructor (with or
        without parameters), the compiler will no longer generate the default for
        you.

        Dan


        Comment

        Working...