Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inherit a constructor with default arguments

Reply
Thread Tools

Inherit a constructor with default arguments

 
 
Martin Magnusson
Guest
Posts: n/a
 
      10-21-2003
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

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      10-21-2003
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.

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      10-21-2003

"Martin Magnusson" <(E-Mail Removed)> wrote in message news:bn464j$m09$(E-Mail Removed)...

> 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.


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      10-21-2003
Martin Magnusson wrote in news:bn464j$m09$(E-Mail Removed):

> 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/
 
Reply With Quote
 
lilburne
Guest
Posts: n/a
 
      10-21-2003
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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
Difference between default arguments and keyword arguments Edward Diener Python 14 04-05-2004 11:26 PM
Constructor & default arguments The Directive C++ 11 01-15-2004 10:44 PM
Default arguments in constructor Gary Labowitz C++ 5 11-20-2003 04:00 PM



Advertisments