Discussion:
Pointers to base class (invoking methods of child classes)
(too old to reply)
Alfonso Morra
2005-09-01 03:12:15 UTC
Permalink
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
Victor Bazarov
2005-09-01 04:02:03 UTC
Permalink
Post by Alfonso Morra
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.
class A {
A() ;
~A() ;
void foo(void) ;
};
class B: public A {
B() ;
~B() ;
int bar(char*) ;
};
class C : public A {
C();
~C();
double foobar(int, int, double ) ;
};
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.
Post by Alfonso Morra
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
Old Wolf
2005-09-01 05:09:27 UTC
Permalink
Post by Alfonso Morra
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"
Post by Alfonso Morra
class A {
A() ;
~A() ;
void foo(void) ;
};
class B: public A {
B() ;
~B() ;
int bar(char*) ;
};
class C : public A {
C();
~C();
double foobar(int, int, double ) ;
};
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";
Victor Bazarov
2005-09-01 14:06:13 UTC
Permalink
Post by Old Wolf
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

Alfonso Morra
2005-09-01 09:50:06 UTC
Permalink
Post by Alfonso Morra
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.
class A {
A() ;
~A() ;
void foo(void) ;
};
class B: public A {
B() ;
~B() ;
int bar(char*) ;
};
class C : public A {
C();
~C();
double foobar(int, int, double ) ;
};
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
Continue reading on narkive:
Loading...