473,383 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Inherit a constructor with default arguments

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

Jul 19 '05 #1
4 4396
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.

Jul 19 '05 #2

"Martin Magnusson" <lo*******@frustratedhousewives.zzn.com> wrote in message news:bn**********@green.tninet.se...
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.
Jul 19 '05 #3
Martin Magnusson wrote in news:bn**********@green.tninet.se:
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/
Jul 19 '05 #4
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?
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: DarkSpy | last post by:
code here: struct A { int _i, _j; A() { cout<<"call default ctor"<<endl; } A(int i, int j) : _i(i), _j(j) { cout<<"call paramater ctor"<<endl; } };
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
11
by: The Directive | last post by:
This code will not compiled: In main function: Dot temp = new Dot( *(new Point( 5, 5 )) ); In Dot class: //Constructor with default arguments. Dot::Dot( Point& point= *(new Point( 5, 5...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
34
by: Jason Heyes | last post by:
Is there a special name for a constructor that does struct-like initialisation? Example: class DefaultCtor { std::string class_name; bool compiler_generated; public:...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.