Topic: Accessing Derived Data/Methods


Author: b91926@fsgi01.fnal.gov (David Sachs)
Date: 16 Apr 1994 19:55:29 -0500
Raw View
oops - I accidently wrote protected_cast instead of "dynamic_cast"
in the preceding article of the thread.




Author: dclausse@sedona.intel.com (David Claussen~)
Date: 14 Apr 1994 22:08:08 GMT
Raw View
Hello,
   I've got a question regarding how I can access data and/or methods from a derived
class when using a pointer to the base class.  A little bit of code might help
clear up what I'm asking:

// If I have a base class:
class base_type
{
   int base_var;
};
// and a derived class:
class derived_type : public base
{
  public:
   int derived_var1;
   char derived_var2;
};

// How can I get at the derived variables if I have a pointer to the base class?
main()
{
  base *base_ptr;
  derived_type *derived;

  derived_ptr = new derived_type;
  base_ptr = derived_ptr;
  derived_ptr = NULL;      // for the purpose of this question, assume I can't get at the derived ptr.

  int x = base_ptr->derived_var1;  // This will NOT work!  How do I get it?
}

Now, the question is, how do I get at derived_var1 (or derived_var2) using base_ptr.
I already know a couple of answers:  I could create a method called get_derived_var1() in
the base class & in the derived class, declare it virtual, and get what I want.
However, for the application, I'd rather not make a get_XXXX() routine for every #$&^#$
element in each one of my derived classes AND base classes (I am deriving MANY classes from the
base class, and they all have unique elements).  I guess I would be satisfied if I could make
one get_XXXX() routine for each element, but I would rather not have to go the
virtual functions route, and have to declare all of these in the base class as well.

I know (or think I know) that I can't directly access the elements using the base_ptr
like this:    cout << base_ptr->derived_var1;  // This doesn't seem to work, for good reason
 or even:
              derived_type *tmp_derived;
              tmp_derived = (derived_type *) base_ptr;
              cout << tmp_derived->derived_var1;  // This pukes too

My apologies if this is incredibly easy to do, but I'm knew to C++ and curious if
there is a tricky way to get at this data without having to write a million get_XXXX()
routines.

Please E-Mail me at dclausse@sedona.intel.com if you have any ideas...

Thanks.

_Dave
--
Dave Claussen:   dclausse@sedona.intel.com
----------------------------------------------------------------------------




Author: b91926@fsgi01.fnal.gov (David Sachs)
Date: 15 Apr 1994 16:43:01 -0500
Raw View
dclausse@sedona.intel.com (David Claussen~) writes:


>Hello,
>   I've got a question regarding how I can access data and/or methods from a derived
>class when using a pointer to the base class.  A little bit of code might help
>clear up what I'm asking:

>// If I have a base class:
>class base_type
>{
>   int base_var;
>};
>// and a derived class:
>class derived_type : public base
>{
>  public:
>   int derived_var1;
>   char derived_var2;
>};

>// How can I get at the derived variables if I have a pointer to the base class?
>main()
>{
>  base *base_ptr;
>  derived_type *derived;

>  derived_ptr = new derived_type;
>  base_ptr = derived_ptr;
>  derived_ptr = NULL;      // for the purpose of this question, assume I can't get at the derived ptr.

>  int x = base_ptr->derived_var1;  // This will NOT work!  How do I get it?
>}

>Now, the question is, how do I get at derived_var1 (or derived_var2) using base_ptr.
>I already know a couple of answers:  I could create a method called get_derived_var1() in
>the base class & in the derived class, declare it virtual, and get what I want.
>However, for the application, I'd rather not make a get_XXXX() routine for every #$&^#$
>element in each one of my derived classes AND base classes (I am deriving MANY classes from the
>base class, and they all have unique elements).  I guess I would be satisfied if I could make
>one get_XXXX() routine for each element, but I would rather not have to go the
>virtual functions route, and have to declare all of these in the base class as well.

>I know (or think I know) that I can't directly access the elements using the base_ptr
>like this:    cout << base_ptr->derived_var1;  // This doesn't seem to work, for good reason
> or even:
>              derived_type *tmp_derived;
>              tmp_derived = (derived_type *) base_ptr;
>              cout << tmp_derived->derived_var1;  // This pukes too

>My apologies if this is incredibly easy to do, but I'm knew to C++ and curious if
>there is a tricky way to get at this data without having to write a million get_XXXX()
>routines.

All you need is a compiler that is up to date in conformance to the still
in progress ANSI/ISO C++ standard - if you can get one for your computer.

The standards committee has added RTTI (Run Time Type Identification)
features to the C++ language.

If you have an up to date compiler, you can safely convert a pointer to
a base class (that has at least 1 virtual function) to a pointer to
a derived class by writing (Please excuse if syntax is a bit off)

d_ptr = protected_cast < d_class* > b_ptr;

If the conversion is not possible (e.g. wrong object type), you will
end up with a NULL pointer.