Sep 1, 2015 at 7:58pm Sep 1, 2015 at 7:58pm UTC
Hi,
I am having a little trouble to get the reference of a derived class. For example -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
class A
{
public :
A() {}
virtual ~A() {}
virtual void func()
{
// something
}
};
class B : public A
{
public :
B():A() {}
virtual ~B() {}
void func()
{
// something
}
void func2()
{
// something else
}
};
class C
{
public :
C() {}
~C() {}
B& getB()
{
return _derived;
}
private :
B _derived;
};
Suppose I have something like this, now If I call like -
1 2
C _caller;
_caller.getB().func2();
I get error: func2 is not a member of A. How to solve it?
EDIT:
Making the getB a pointer and dynamic_cast solved the problem.
1 2 3 4
B* getB()
{
return &_derived;
}
Thanks in advance.
Last edited on Sep 1, 2015 at 9:30pm Sep 1, 2015 at 9:30pm UTC
Sep 1, 2015 at 8:34pm Sep 1, 2015 at 8:34pm UTC
you should put your method for a and b public .
also b constructor does not call a constructor.
try that , then if it does not work , tell us.
Sep 1, 2015 at 10:26pm Sep 1, 2015 at 10:26pm UTC
Yes, it also works. Thanks for the reply.