473,700 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2286

"Flzw" <fl****@wanadoo .fr> wrote in message
news:41******** *************** @news.wanadoo.f r...
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::Derive d() : 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*****@hotmai l.com> wrote in message
news:WQ******** *************@b gtnsc05-news.ops.worldn et.att.net...

"Flzw" <fl****@wanadoo .fr> wrote in message
news:41******** *************** @news.wanadoo.f r...
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******@gasca d.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.f r...
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**********@Ho me.com
Jul 22 '05 #8

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

"Flzw" <fl****@wanadoo .fr> wrote in message
news:41******** *************** @news.wanadoo.f r...
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

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

Similar topics

0
4250
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 (with no responces associated) :
8
1414
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 constructor would always need to be called before anything else seeing as the subclass may well depend on functionality and variables created in the base class.
4
4618
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
1593
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 app with; D m_D = new D(tkn); public class A : MarshalByRefObject public A () <--------------------- public class B : A public B () <----------------------
3
2045
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 extend it into an object of class Item. What is the best practice to extend the base class object into a class that inherits from it? Thanks,
12
5805
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. The subclass's constructor sets this variable to a real value. So first the variable is set to 0, the variable is never used before it once again gets set to a real value. It's a waste. Any ideas anyone?
6
2496
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
1876
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 derived class to call the constructors for the base class using also the same kind of overloading. like this: class BASE{ public: BASE(); BASE(int x);
26
5363
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
0
8721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8964
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7804
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4400
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4653
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2385
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2024
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.