Question

i have an accounts class from that i have 3 types of accounts savings, credit, and homeloan.

i created a binary search tree to hold all the accounts as type account

how do i now access the methods of the subclasses depending on the type of object?

have resolved all errors with syntax and codeing but this.

been racking my head for 2 days . does anyone know how this is done ?

Was it helpful?

Solution

The simple answer is, if you need to access the derived class functionality from a base class pointer, you have a design problem. In principle, you shouldn't need to know. If you do, something is wrong. You're supposed (in a pure sense) to call virtual functions from the base class interface, and have the derived classes implement their overrides such that they perform correctly.

Now then, sometimes, practically, you have to. So there is the possibility of a downcast. If you have Run Time Type information in your build, you can do a dynamic_cast<type*> and if the pointer you get back is non-null, then you have an instance of that type.

If you do go down this path, wrap it in something neat and don't let it proliferate - it can get messy. I suggest you see if there isn't a better way, using polymorphism.

Have fun!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top