Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 1st, 2005, 04:25 AM
Alfonso Morra
Guest
 
Posts: n/a
Default 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

  #2  
Old September 1st, 2005, 05:15 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Pointers to base class (invoking methods of child classes)

Alfonso Morra wrote:[color=blue]
> 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[/color]

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.
[color=blue]
> How can I invoke foobar() on base_ptr ?[/color]

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

or

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

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

V


  #3  
Old September 1st, 2005, 06:15 AM
Old Wolf
Guest
 
Posts: n/a
Default Re: Pointers to base class (invoking methods of child classes)

Alfonso Morra wrote:[color=blue]
> 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.[/color]

Explain what you mean by "dosen't work"
[color=blue]
>
> 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 ?[/color]

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";

  #4  
Old September 1st, 2005, 10:55 AM
Alfonso Morra
Guest
 
Posts: n/a
Default Re: Pointers to base class (invoking methods of child classes)



Alfonso Morra wrote:
[color=blue]
> 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
>[/color]
Thanks guys

  #5  
Old September 1st, 2005, 03:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Pointers to base class (invoking methods of child classes)

Old Wolf wrote:[color=blue]
> Alternatively:
>
> C *ptr = dynamic_cast<C *>(base_ptr);
> if (ptr)
> ptr->foobar();
> else
> cout << "base_ptr did not point to a C";
>[/color]

Alternatively

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

(avoids placing 'ptr' into the surrounding scope)

V
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles