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;
}
};

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.