Re: [C++] Inheritance Question: Constructors for Derived classes?
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 12/28/04
- Next message: Chris \( Val \): "Re: Converting int to string"
- Previous message: Ben Cottrell: "Re: console app and keyboard"
- Next in thread: Jonathan Mcdougall: "Re: [C++] Inheritance Question: Constructors for Derived classes?"
- Maybe reply: Jonathan Mcdougall: "Re: [C++] Inheritance Question: Constructors for Derived classes?"
- Maybe reply: Francis Glassborow: "Re: [C++] Inheritance Question: Constructors for Derived classes?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 28 Dec 2004 11:50:18 +1100
"entropy123" <email_entropy123@yahoo.com> wrote in message
news:1104194263.307460.149630@z14g2000cwz.googlegroups.com...
| Hi All
|
| My question is the following: Is there a way to make a constructor for
| the derived class with additional derived class specific arguments
| while also making use of the derived class constructor:
|
| For example: In the code below the derived class constructor ALSO takes
| an int value in addition to the _str taken by the base class. The
| compiler produces an error when I attempt to implement the derived
| class as in my code snippet. Is there some other way?
|
| Thanks,
| ent
|
| class Base {
| public:
| Base(string in_str);
| ~Base();
| string getStr();
|
| private:
| string _str;
|
| }
|
| class Derived : public Base {
| public:
| Derived ( int in_data, string in_str ); //error
| private:
| int _data;
| }
Use initialisation lists:
class Base
{
public:
Base( string in_str ) : _str( in_str ) {}
private:
string _str;
};
class Derived : public Base
{
public:
Derived( int in_data, string in_str )
: Base( in_str ) {}
private:
int _data;
};
Cheers.
Chris Val
- Next message: Chris \( Val \): "Re: Converting int to string"
- Previous message: Ben Cottrell: "Re: console app and keyboard"
- Next in thread: Jonathan Mcdougall: "Re: [C++] Inheritance Question: Constructors for Derived classes?"
- Maybe reply: Jonathan Mcdougall: "Re: [C++] Inheritance Question: Constructors for Derived classes?"
- Maybe reply: Francis Glassborow: "Re: [C++] Inheritance Question: Constructors for Derived classes?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|