Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointers to base class (invoking methods of child classes)

Reply
Thread Tools

Pointers to base class (invoking methods of child classes)

 
 
Alfonso Morra
Guest
Posts: n/a
 
      09-01-2005
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-01-2005
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


 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      09-01-2005
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";

 
Reply With Quote
 
Alfonso Morra
Guest
Posts: n/a
 
      09-01-2005


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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-01-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
How to prevent a function in base class being overloaded from child class modemer C++ 20 03-22-2005 04:51 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments