Base ptr to access the derived members

S

Subra

In a interview I was asked how to make the below code work ( without
changing the base class ) ?

#include <iostream>
#include <string>
using namespace std;

class base
{
public:
virtual void m1() { cout <<"m1 " << endl ; }
};

class der : public base
{
public:
virtual void m2() { cout <<"m2 " << endl ; } //m2 is not there in base
class
};

int main() {
base* ptr;
der obj;
ptr = &obj;
ptr->m2(); // base ptr is trying to call m2 , and hence the code
cant compile
}
 
M

Marcel Müller

Subra said:
class base
{
public:
virtual void m1() { cout <<"m1 " << endl ; }
};

class der : public base
{
public:
virtual void m2() { cout <<"m2 " << endl ; } //m2 is not there in base
class
};

int main() {
base* ptr;
der obj;
ptr = &obj;
ptr->m2(); // base ptr is trying to call m2 , and hence the code
cant compile
}

You need a downcast.

If you are really sure, that *ptr is of type der a static cast is the
first choice.

static_cast<der*>(ptr)->m2();

If ptr might point to some other class and that does not derive from
der, then the above code has undefined behavior. In this case you can
use a dynamic cast.

der* ptr2 = dynamic_cast<der*>(ptr);

This checks whether *ptr is really of type der and otherwise sets ptr2
to NULL. Of course, you have to deal with that case in your code to
avoid to dereference a NULL pointer.


Marcel
 
J

Jeff Flinn

Marcel said:
You need a downcast.

If you are really sure, that *ptr is of type der a static cast is the
first choice.

static_cast<der*>(ptr)->m2();

If ptr might point to some other class and that does not derive from
der, then the above code has undefined behavior. In this case you can
use a dynamic cast.

der* ptr2 = dynamic_cast<der*>(ptr);

This checks whether *ptr is really of type der and otherwise sets ptr2
to NULL. Of course, you have to deal with that case in your code to
avoid to dereference a NULL pointer.

I think you fell for the trick question. ;-) Just:

obj.m2();

Jeff
 
P

Paul N

The problem is underspecified, and any answer is a guess.

Well, yes, but this is an interview rather than an exam. So you can
ask for more details, or explain what assumptions you're using, give
several different answers, etc. All of which might show your abilities
(or lack of them) better than a simple question that would either be
right or wrong.

Of course the company may not have planned it that way.
 
F

fenghuang

I know this is ugly, but it may be the answer. Thank You!

typedef void (*FunType)(base*);
typedef FunType* VTableType;

int main() {
base* ptr;
der obj;
ptr = &obj;
VTableType pTable = *(VTableType*)ptr; // get virtual table pointer
FunType pFun = pTable[1]; //the first is 'm1', and the
second is 'm2'
pFun(ptr); //call 'm2' with 'this'

return 0;
}
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top