Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Promiscuous Robot

C++ quickie.

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

Say I have a class A and a class derived from A called B. (ie. class A; class B : public A I have a function that accepts a const A&, now I know that B is a type of A, so is there a way I can use an object of B as a paramter to this function? Here''s my guess as to how to do it:
  
// function

void somefunc (const A& obj)
{
//stuff

}

void main ()
{
   B ihopethisworks;

   somefunc (ihopethisworks::A);
}
  
If this is not the way to do it, is there a way at all??

Share this post


Link to post
Share on other sites
Advertisement
You should just be able to do:

  
class A {};
class B : public A {};

void somefunc(const A &obj)
{
}

void main()
{
B an_object;

somefunc(an_object);
}


JoeDark

Share this post


Link to post
Share on other sites
quote:
Original post by Promiscuous Robot

Thats unexpected.



Actually it's not. Object B "ISA" Object A. Your function takes objects of type A. Since B is an A then there's no problem.

quote:

If the function took a the full object and not just a reference that wouldn't work right?


Same deal. Object B is of type A so the function sees it as type A.



-------
Andrew

Edited by - acraig on January 26, 2001 8:01:42 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!