Inherit a constructor with default arguments

M

Martin Magnusson

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
 
A

Artie Gold

Martin said:
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
 
R

Ron Natalie

Martin Magnusson said:
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.
 
R

Rob Williscroft

Martin Magnusson wrote in
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.
 
L

lilburne

Martin said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top