I've class G.
class B : public G
class A : public B
class D : public G
class C : public D
class F : public G
class E : public F
From a different class, I've an access to G (G sorry!!). I've to get some memeber of class A (for example, string m_Astr).
How can I access the A string? If I use pure virtual in class G, I'll have to implement this getStr function in E and C in addition to the implementation of A, and I need it for A only.
please help
                        
If you know (somehow) that the object pointed to is actually an instance of an
A, then you can usestatic_cast:However, if the object pointed to by
pabove doesn't instantiate anA, then the above code will yield undefined behavior.A more safe approach would be to use
dynamic_cast,Here,
awill beNULLifpisn't actually anA. However,dynamic_castcan only be used in this manner with polymorphic classes: the base class must have at least onevirtualfunction. Avirtualdestructor would do nicely here.