Jump to content
  • Advertisement
Sign in to follow this  
Turt99

Pointers to Base Class

This topic is 3348 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement


class Base
{
public:
virtual void doSomething(){actualDoSomething();}
void actualDoSomething();

};

class Derived1
{
public:
void doSomething();

};

class Derived2
{
public:


};

//some cpp file
void 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Interesting, I guess it might be a better idea to avoid the casting, issue. I was focusing on one side of the issue. I think you post has pointed me to a easier solution. Thanks

NOTE: This is for Black Knight

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!