Advertisement

C++ quickie.

Started by January 26, 2001 06:32 PM
2 comments, last by Promiscuous Robot 24 years ago
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??
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
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
Advertisement
Oh.

Thats unexpected.

If the function took a the full object and not just a reference that wouldn''t work right?
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
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

This topic is closed to new replies.

Advertisement