Loading ...
Sorry, an error occurred while loading the content.

RE: Inheritance Issue ....

Expand Messages
  • Hubbell, Sean (GDIS)
    ... Please look at static_cast, for example: static_cast (&c) A non-null pointer will be returned if this cast is safe... so, if (Car* ptr =
    Message 1 of 3 , Jun 1, 2001
    • 0 Attachment
      > class Vehicle {
      >
      > // Some Members.
      > }
      >
      > class Car::public Vehicle {
      >
      > // Some Members.
      > }
      >
      > class Bus::public Vehicle {
      >
      > // Some Members.
      > }
      >
      > main()
      > {
      >
      > Vehicle* v;
      > Car c;
      > Bus b;
      >

      Please look at static_cast, for example: static_cast<Vehicle>(&c)
      A non-null pointer will be returned if this cast is safe...

      so,
      if (Car* ptr = static_cast<Car>(&c))
      {
      someFunc (*ptr); // passes Car
      }
      else if (Bus* ptr = static_cast<Car>(&c))
      {
      someFunc (*ptr); // passes Bus
      }

      > v = &c; //How do I know that what I have in v is a pointer to c
      >
      > // Similarly
      >
      > v = &b; //How do I know that what I have in v is a pointer to b
      >
      > someFunc(*v); // So I get an error there is no func which takes
      > Vehicle reference.
      > }
      >
      > someFunc(Car& c)
      > {
      > do something;
      > }
      >
      > someFunc(Bus& b)
      > {
      > do something;
      > }
      >
      > Basically I would like to get a reference to the corresponding derived
      > class as stored in the base class.
      > Is this possible ?
      > (I know about virtual functions and all that. My case is I do want to
      > call someFunc exactly as above)
      >
      > Best Regards
      > Naren
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.