Accessing base class function

A

Angus

I have a base class which has a function GetThingID which returns a
integer value. The ctor of the base class sets the value.

In my derived class I want to use this function GetThingID. I have
NOT decalred a function GetThingID in my derived class. How how do I
construct my derived class to pass a value so as to populate the
private base class member?
 
V

Victor Bazarov

Angus said:
I have a base class which has a function GetThingID which returns a
integer value. The ctor of the base class sets the value.

In my derived class I want to use this function GetThingID. I have
NOT decalred a function GetThingID in my derived class.

Your derived class inherits that function.
How how do I
construct my derived class to pass a value so as to populate the
private base class member?

Initialise the base class in the appropriate place in the derived
class' constructor initialiser list .

V
 
B

Barry

Angus said:
I have a base class which has a function GetThingID which returns a
integer value. The ctor of the base class sets the value.

In my derived class I want to use this function GetThingID. I have
NOT decalred a function GetThingID in my derived class. How how do I
construct my derived class to pass a value so as to populate the
private base class member?

I suggest you change it into protected

class Base
{
public:
Base(int i) : i_(i) {}
protected:
int GetThingID() { return i_; }
private:
int i_;
};

class Derived : public Base
{
void f() { int id = GetThingID(); }
};

Or you can grant /Derived/ to be friend of /Base/
but not so straightforward, since /Derived/ is already a child of /Base/

class Base
{
friend class Derived;
public:
Base(int i) : i_(i) {}
private:
int GetThingID() { return i_; }
int i_;
};

class Derived : public Base
{
void f() { int id = GetThingID(); }
};
 
Z

Zeppe

Angus said:
I have a base class which has a function GetThingID which returns a
integer value. The ctor of the base class sets the value.

In my derived class I want to use this function GetThingID. I have
NOT decalred a function GetThingID in my derived class. How how do I
construct my derived class to pass a value so as to populate the
private base class member?

The ID is already populated by the base class constructor. If you don't
explicitly call any base constructor in the derived constructor
initialisation list, as victor suggested, the default base constructor
is called automatically. Just access the ID parameter via the
getThingID, that should be accessible from the derived class (I suppose
it's a public member).

Regards,

Zeppe
 
J

Juha Nieminen

Angus said:
How how do I
construct my derived class to pass a value so as to populate the
private base class member?

You mean something like this?

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top