A
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.
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
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