Inherit a constructor with default arguments

Discussion in 'C++' started by Martin Magnusson, Oct 21, 2003.

  1. I have an abstract class MaxNode with a single constructor looking like
    this:

    MaxNode( std::string name = "NN" );

    The class MaxLeaf is derived from MaxNode, and doesn't define any
    explicit constructor of its own.

    Now shouldn't it be possible to declare something like this:

    MaxLeaf* north = new MaxLeaf( "North" );

    g++ tells me that there is no matching function for call to
    `MaxLeaf::MaxLeaf(const char[6])'
    candidates are: MaxLeaf::MaxLeaf()
    MaxLeaf::MaxLeaf(const MaxLeaf&)

    What am I doing wrong?

    / martin
    Martin Magnusson, Oct 21, 2003
    #1
    1. Advertising

  2. Martin Magnusson

    Artie Gold Guest

    Martin Magnusson wrote:
    > I have an abstract class MaxNode with a single constructor looking like
    > this:
    >
    > MaxNode( std::string name = "NN" );
    >
    > The class MaxLeaf is derived from MaxNode, and doesn't define any
    > explicit constructor of its own.
    >
    > Now shouldn't it be possible to declare something like this:
    >
    > MaxLeaf* north = new MaxLeaf( "North" );
    >
    > g++ tells me that there is no matching function for call to
    > `MaxLeaf::MaxLeaf(const char[6])'
    > candidates are: MaxLeaf::MaxLeaf()
    > MaxLeaf::MaxLeaf(const MaxLeaf&)
    >
    > What am I doing wrong?
    >

    Constructors are not inherited. Sorry.
    You'll have to define an appropriate one for the subclass.

    HTH,
    --ag




    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.
    Artie Gold, Oct 21, 2003
    #2
    1. Advertising

  3. Martin Magnusson

    Ron Natalie Guest

    "Martin Magnusson" <> wrote in message news:bn464j$m09$...

    > g++ tells me that there is no matching function for call to
    > `MaxLeaf::MaxLeaf(const char[6])'
    > candidates are: MaxLeaf::MaxLeaf()
    > MaxLeaf::MaxLeaf(const MaxLeaf&)

    That message implies you did NOT define a constructor at all. The two shown are the
    compiler generated defaults most likely.

    You want to show us the COMPLETE class declaration.
    Ron Natalie, Oct 21, 2003
    #3
  4. Martin Magnusson wrote in news:bn464j$m09$:

    > I have an abstract class MaxNode with a single constructor looking like
    > this:
    >
    > MaxNode( std::string name = "NN" );
    >
    > The class MaxLeaf is derived from MaxNode, and doesn't define any
    > explicit constructor of its own.
    >
    > Now shouldn't it be possible to declare something like this:
    >
    > MaxLeaf* north = new MaxLeaf( "North" );
    >


    Maybe it should, but that isn't how the language currently works,
    constructers aren't inherited, they're created by you, or in two
    special cases (default and copy) by the compiler (if you don't that
    is), these are the two listed in g++'s error message bellow.


    >g++ tells me that there is no matching function for call to
    > `MaxLeaf::MaxLeaf(const char[6])'
    > candidates are: MaxLeaf::MaxLeaf()
    > MaxLeaf::MaxLeaf(const MaxLeaf&)
    >


    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Oct 21, 2003
    #4
  5. Martin Magnusson

    lilburne Guest

    Martin Magnusson wrote:

    > I have an abstract class MaxNode with a single constructor looking like
    > this:
    >
    > MaxNode( std::string name = "NN" );
    >
    > The class MaxLeaf is derived from MaxNode, and doesn't define any
    > explicit constructor of its own.
    >
    > Now shouldn't it be possible to declare something like this:
    >
    > MaxLeaf* north = new MaxLeaf( "North" );
    >


    Others have spoken about ctor inheritance (or rather the
    lack of) but I have one question - what is the utility of
    the default argument? What is wrong with having two
    constructors:

    MaxNode::MaxNode()
    : name_("NN")
    {
    }

    MaxNode::MaxNode(std::string name)
    : name_(name)
    {
    }

    instead of one constructor that masquerades as two?
    lilburne, Oct 21, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary Labowitz

    Default arguments in constructor

    Gary Labowitz, Nov 19, 2003, in forum: C++
    Replies:
    5
    Views:
    10,995
    Gary Labowitz
    Nov 20, 2003
  2. The Directive

    Constructor & default arguments

    The Directive, Jan 14, 2004, in forum: C++
    Replies:
    11
    Views:
    923
    Peter Koch Larsen
    Jan 15, 2004
  3. Edward Diener
    Replies:
    14
    Views:
    4,935
    Josiah Carlson
    Apr 6, 2004
  4. tutmann
    Replies:
    4
    Views:
    435
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,218
Loading...

Share This Page