Pointers to base class (invoking methods of child classes)

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
 
V

Victor Bazarov

Alfonso said:
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
 
O

Old Wolf

Alfonso said:
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";
 
A

Alfonso Morra

Alfonso said:
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
 
V

Victor Bazarov

Old said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top