Jump to content
  • Advertisement
Sign in to follow this  
Sammy0037

casts

This topic is 4859 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

Is using a cast a frowned upon practice? I have a function that takes a base class pointer as a parameter (common base, from which several are derived). A base pointer is used so as to allow all the derived classes to use the same function. I suppose making a temp base pointer from the derived and passing that instead would work. Cheers

Share this post


Link to post
Share on other sites
Advertisement
Seeing as you don't need a cast to go from derived to base, I don't see a problem here. Though casts in general are a warning sign.

Share this post


Link to post
Share on other sites
Correction.. Im not using pointers, per se, but references.

This should not work without casts. Right?

baseclass base;
derivedclass1 derived1;
derivedclass2 derived2;
derivedclass3 derived3;

DoSomething(derived1, derived2);
DoSomething(derived2, derived3);

void DoSomething(baseclass &a, baseclass &b)
{
//do something with a and b
};

////
Would an alternative way be to send derived pointers... which are received as base pointers? Does this work?

DoSomething(*derived2, *derived3);

void DoSomething(baseclass *a, baseclass *b)
{
//do something with a and b
};

Share this post


Link to post
Share on other sites
Instead of typing your code into a message board and asking other people to compile it, why don't you download a compiler and see what it has to say about your code?

Share this post


Link to post
Share on other sites
You don't need casts to use a derived class with a base class reference either. It should just automagically work.

Share this post


Link to post
Share on other sites
Sorry, no I didnt post actual code. And I wasnt asking anyone to compile anything or debug. It was more of getting a general idea of what Im looking to do... to see if I am going about it the right way.

Automagic is nice, when it works... which it seems to so far now. Thanks ;o)

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!