473,320 Members | 2,098 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,320 software developers and data experts.

Base clase constructor

Probably a stupid question but, how do I pass arguments to the base class
constructor when I create an Instance of a derived class ?
Jul 22 '05 #1
10 2254

"Flzw" <fl****@wanadoo.fr> wrote in message
news:41***********************@news.wanadoo.fr...
Probably a stupid question but, how do I pass arguments to the base class
constructor when I create an Instance of a derived class ?


I believe this is what you're looking for:

class Derived : public Base
{
Derived( int x ) : Base( x );
};

-Howard
Jul 22 '05 #2
Flzw wrote:

Probably a stupid question but, how do I pass arguments to the base class
constructor when I create an Instance of a derived class ?


Does your textbook not cover this?
(You cannot learn a language as complex as C++ with a textbook)

To answer the question:
By specifying an initializer list

class Base
{
public:
Base( int i );
};

class Derived : public Base
{
public:
Derived();
};

Derived::Derived() : Base( 5 ) // initializer list to initialize the
// Base part by passing 5 to the ctor
{
}
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3
Karl Heinz Buchegger wrote:

Flzw wrote:

Probably a stupid question but, how do I pass arguments to the base class
constructor when I create an Instance of a derived class ?


Does your textbook not cover this?
(You cannot learn a language as complex as C++ with a textbook)

****

typo: without

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

"Howard" <al*****@hotmail.com> wrote in message
news:WQ*********************@bgtnsc05-news.ops.worldnet.att.net...

"Flzw" <fl****@wanadoo.fr> wrote in message
news:41***********************@news.wanadoo.fr...
Probably a stupid question but, how do I pass arguments to the base class constructor when I create an Instance of a derived class ?


I believe this is what you're looking for:

class Derived : public Base
{
Derived( int x ) : Base( x );
};

-Howard


I meant this:

Derived( int x ) : Base( x ) { };

(I think the initializer has to go with the *definition* of the constructor,
and my first post just had a declaration, not a definition.)

-Howard
Jul 22 '05 #5

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
Karl Heinz Buchegger wrote:

Flzw wrote:

Probably a stupid question but, how do I pass arguments to the base class constructor when I create an Instance of a derived class ?


Does your textbook not cover this?
(You cannot learn a language as complex as C++ with a textbook)

****

typo: without

--


I was wondering... :-)


Jul 22 '05 #6
Howard wrote:

"Flzw" <fl****@wanadoo.fr> wrote in message
news:41***********************@news.wanadoo.fr...
Probably a stupid question but, how do I pass arguments to the base class
constructor when I create an Instance of a derived class ?


I believe this is what you're looking for:

class Derived : public Base
{
Derived( int x ) : Base( x );
};


That's a syntax error.

Either you define the ctor inline, then the initializer list is also
defined 'inline'.
Or you don't, then the initializer list is also not 'inline'.

In simpler words: The intializer list goes always with the constructors
implementation.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #7
On Tue, 28 Sep 2004 17:47:52 +0200, "Flzw" <fl****@wanadoo.fr> wrote:
Probably a stupid question but, how do I pass arguments to the base class
constructor when I create an Instance of a derived class ?


In the initialization list of the derived class' constructor, e.g.:

class Base
{
int x;
public:
Base(int a) : x(a) {}
};

class Derived
{
int y;
public:
Derived(int b, int c) : Base(b), y(c) {}
};
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #8

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
Howard wrote:

"Flzw" <fl****@wanadoo.fr> wrote in message
news:41***********************@news.wanadoo.fr...
Probably a stupid question but, how do I pass arguments to the base class constructor when I create an Instance of a derived class ?


I believe this is what you're looking for:

class Derived : public Base
{
Derived( int x ) : Base( x );
};


That's a syntax error.


Yeah, I saw that the instant I hit Send. :-) (I also corrected myself.
Hopefully I didn't screw up my correction as well. Some days it just don't
pay to open my big fat mouth.)

-Howard
Jul 22 '05 #9
Howard wrote:
[snip]
Yeah, I saw that the instant I hit Send. :-)


Don't you hate it when that happens? I do!

That's really interesting: I can proofread as long
as I want and don't see the simplest mistakes. 1/10
second after hitting send, I immediatly know that I
made a mistake.
Same thing with programming. I can spend literally hours
in proofreading a code section and don't see the problem.
But as soon as I fire up the debugger and the breakpoint
at the start of the section is reached, I know immediatly
what's wrong. I vote for editors with builtin debuggers :-)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10
"Howard" <al*****@hotmail.com> wrote in message news:<fU*********************@bgtnsc04-news.ops.worldnet.att.net>...
"Howard" <al*****@hotmail.com> wrote in message
news:WQ*********************@bgtnsc05-news.ops.worldnet.att.net...
.... I meant this:

Derived( int x ) : Base( x ) { }; ^
just a small remark: this semicolon is not necessary.
(I think the initializer has to go with the *definition* of the constructor,
and my first post just had a declaration, not a definition.)

-Howard


Best regards,

Marcelo Pinto
Jul 22 '05 #11

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

Similar topics

0
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message...
8
by: Simon | last post by:
Hi everyone, Quick question: If I don't use base() in a subclass's construcor, will the base classes constructor be called at any point. It's just, I would have thought that the base class...
4
by: Xavier | last post by:
Hi, I have a question, in a "dreaded diamond" situation, regarding the following code: ---- Begin code #include <iostream> using namespace std;
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
3
by: James | last post by:
Hi, I have two classes, a base clase ItemType and a class which inherits from it called Item. During the runtime of my program, I create an instance of ItemType, then later, I would like to...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
1
by: mwebel | last post by:
Hi ikind of remember this being easy but cant find it on google: i have a base class and a derived class. The base class has two constructors normal and int overloaded one. thing is i want the...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.