Pointers to Base Class
I have a function that takes in a base class pointer, this function does the exact same thing for all derived classes except 1, I want to say if the object is a certain derived class then do 1 extra step. Is that possible?
Virtual functions don't really help because the change involves a processing another object in a different way.
I hope that makes sense, I'm having a hard time googling this because I don't know how to ask.
Is this C++? I'm assuming based on the way you described it that it is. If this is the case, then you may want to look into run-time type information.
class Base{public:virtual void doSomething(){actualDoSomething();}void actualDoSomething();};class Derived1{public:void doSomething();};class Derived2{public:};//some cpp filevoid Derived1::doSomething(){// do something more than the base method// call base method actualDoSomething();}void method(Base* basePointer){basePointer->doSomething();}
Would this work?It just calls the virtual function if its implemented in derived classes the derived class does something else and then calls the base method to do default behaviour.This way you don't have to do type checking or stuff.
Edit: On second thought if its doing the same thing for all derived classes except on just dont write the virtual function for derived classes that do the same thing :) leave it in the base method class and only write the virtual method in the derived class that does the special stuff.But you still want the base code to run for the derived class right so this might not be what you want.
Quote:Original post by Turt99
Sorry, yes this is C++.
Thank you, I think the link you have sent should give me what I need
One thing to remember is that you typically have to explicitly enable RTTI in your compiler in order to use it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement