473,489 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointers to base class (invoking methods of child classes)

Hi,

I have a variable that stores a pointer to a base class. I am using this
variable to store pointers to objects of the base class, as well as
pointers to other derived classes.

However, the derived classes have methods (not available on the base
class) that I would like to invoke. I thought I could simply cast the
pointer to the appropriate derived class and access the methods this way
- but that dosen't work.

Example :

class A {
public:
A() ;
~A() ;
void foo(void) ;
};

class B: public A {
public:
B() ;
~B() ;
int bar(char*) ;
};
class C : public A {
public:
C();
~C();

double foobar(int, int, double ) ;
};

//variable to hold ptr to base class:

A *base_ptr = new C; // ptr to A or B or C can be stored in variable

How can I invoke foobar() on base_ptr ?
tkx

Sep 1 '05 #1
4 2690
Alfonso Morra wrote:
I have a variable that stores a pointer to a base class. I am using
this variable to store pointers to objects of the base class, as well
as pointers to other derived classes.

However, the derived classes have methods (not available on the base
class) that I would like to invoke. I thought I could simply cast the
pointer to the appropriate derived class and access the methods this
way - but that dosen't work.

Example :

class A {
public:
A() ;
~A() ;
void foo(void) ;
};

class B: public A {
public:
B() ;
~B() ;
int bar(char*) ;
};
class C : public A {
public:
C();
~C();

double foobar(int, int, double ) ;
};

//variable to hold ptr to base class:

A *base_ptr = new C; // ptr to A or B or C can be stored in variable
Since you allocate using 'new' and keep the base class pointer, you
will need to delete it at some point. Your base class _destructor_
MUST be virtual to avoid undefined behaviour.
How can I invoke foobar() on base_ptr ?


dynamic_cast<C*>(base_ptr)->foobar(...

or

static_cast<C*>(base_ptr)->foobar(...

since you know that the original class _was_ 'C'.

V
Sep 1 '05 #2
Alfonso Morra wrote:
Hi,

I have a variable that stores a pointer to a base class. I am using
this variable to store pointers to objects of the base class, as well
as pointers to other derived classes.

However, the derived classes have methods (not available on the
base class) that I would like to invoke. I thought I could simply
cast the pointer to the appropriate derived class and access the
methods this way - but that dosen't work.
Explain what you mean by "dosen't work"

Example :

class A {
public:
A() ;
~A() ;
void foo(void) ;
};

class B: public A {
public:
B() ;
~B() ;
int bar(char*) ;
};
class C : public A {
public:
C();
~C();

double foobar(int, int, double ) ;
};

//variable to hold ptr to base class:

A *base_ptr = new C; // ptr to A or B or C can be stored in variable

How can I invoke foobar() on base_ptr ?


dynamic_cast<C &>(*base_ptr).foobar()

This will throw an exception if base_ptr doesn't point to a C.
Alternatively:

C *ptr = dynamic_cast<C *>(base_ptr);
if (ptr)
ptr->foobar();
else
cout << "base_ptr did not point to a C";

Sep 1 '05 #3


Alfonso Morra wrote:
Hi,

I have a variable that stores a pointer to a base class. I am using this
variable to store pointers to objects of the base class, as well as
pointers to other derived classes.

However, the derived classes have methods (not available on the base
class) that I would like to invoke. I thought I could simply cast the
pointer to the appropriate derived class and access the methods this way
- but that dosen't work.

Example :

class A {
public:
A() ;
~A() ;
void foo(void) ;
};

class B: public A {
public:
B() ;
~B() ;
int bar(char*) ;
};
class C : public A {
public:
C();
~C();

double foobar(int, int, double ) ;
};

//variable to hold ptr to base class:

A *base_ptr = new C; // ptr to A or B or C can be stored in variable

How can I invoke foobar() on base_ptr ?
tkx

Thanks guys

Sep 1 '05 #4
Old Wolf wrote:
Alternatively:

C *ptr = dynamic_cast<C *>(base_ptr);
if (ptr)
ptr->foobar();
else
cout << "base_ptr did not point to a C";


Alternatively

if (C *ptr = dynamic_cast<C*>(base_ptr))
ptr->foobar();
else
cout << "bleh";

(avoids placing 'ptr' into the surrounding scope)

V
Sep 1 '05 #5

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

Similar topics

1
1672
by: TheOne | last post by:
I have two classes: class OntologyParser { ... protected: virtual void startElement(void *userData, const char *name, const char **atts); virtual void endElement(void *userData, const char...
4
9761
by: james | last post by:
I have a custom UserControl, which can have many sub class levels derived from it. I want to be able to discover all the components at Load time, but the only components I can see from the base...
10
8636
by: gmtonyhoyt | last post by:
It's been mentioned to me that, in standard c, that you can have structures behave very much like classes in relation to something to do with function calls within a structure. Now, I didn't get...
4
12857
by: Néstor Marcel Sánchez Ahumada | last post by:
In a method declaration the 'sealed' keyword must be used with the 'override' keyword to avoid further overriding. Thus it can't be used in base classes. Why? This would be a good enhancement for...
25
3766
by: lucas_denoir | last post by:
I'm having some serious trouble accessing a virtual method of a base class - that is not the immidate base class. This is the basic situation that I have: ...
12
1962
by: ex_ottoyuhr | last post by:
I have a situation more or less as follows, and it's causing me no end of trouble; I'd appreciate anyone's advice on the matter... Given these classes: class BedrockCitizen { ... }; class...
12
3404
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
1
6503
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the...
19
2206
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
0
7108
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,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5445
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,...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
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...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
272
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...

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.