0

Hi all,

Suppose, I have the following classes B, D1 and D2 as defined below.
Also, I have function test which have only pointer to the base class and say
key which will tell D1 or D2 to create.

void test(B *b, const std::string &key)
{
...
}

I would like to be able inside of test to call fd1 of D1 or fd2 of D2 depending on key.
Based on relationships between B,D1,D2 I cannot do it. Do you have any suggestion how I would do it?

Thank you in advance,
nuclph.

class B
{
public:
virtual void print() = 0;
};


class D1 : public B
{
public:
void print()
{
cout << "in class D1" << endl;
}
void fd1()
{
cout << "class D1 function" << endl;
}
};


class D2 : public B
{
public:
void print()
{
cout << "in class D2" << endl;
}
void fd2()
{
cout << "class D2 function" << endl;
}
};

Edited by happygeek: fixed formatting

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by nucleon
0

You're supposed to use code tags when posting code.
You'll need to show what you've attempted so far.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.