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

Inheritance Issue ....

Expand Messages
  • Narendranath Reddy
    Hi, I have shown a simulation of my current hiearchy below class Vehicle { // Some Members. } class Car::public Vehicle { // Some Members. } class Bus::public
    Message 1 of 3 , May 31, 2001
    • 0 Attachment
      Hi,
      I have shown a simulation of my current hiearchy below

      class Vehicle {

      // Some Members.
      }

      class Car::public Vehicle {

      // Some Members.
      }

      class Bus::public Vehicle {

      // Some Members.
      }

      main()
      {

      Vehicle* v;
      Car c;
      Bus b;

      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
    • Narendranath Reddy
      Teribbly sorry I sent to the wrong place. Best Regards Naren
      Message 2 of 3 , May 31, 2001
      • 0 Attachment
        Teribbly sorry I sent to the wrong place.

        Best Regards
        Naren
      • 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 3 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.