• ### Announcements

#### Archived

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

# C++ quickie.

## 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 on other sites
JoeDark    122
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 on other sites
Oh.

Thats unexpected.

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

##### Share on other sites
acraig    471
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