Topic: Upcasting Classes Help
Author: "Nate Lewis" <nlewis@mindspring.com>
Date: 1998/09/24 Raw View
Benjamin M. Stocks wrote in message <6u9ddl$gbk@wiscnews.wiscnet.net>...
>If I have a base class, call it 'A'. Then I derive class 'B' and
class 'C'
>from it. Now to store B and C in the same data structure I need to
upcast
>instances of B and C to class A and push them into a list. When I
reference
>them in the list, ex an iterator, is there anyway to tell the compiler
to
>use the derived member functions (from B and C) and not the base class
>member functions or am I limited to the member functions of the class I
used
>to intialize the list?
If this list stores As, you're copying out ('slicing') the A-ness of
your Bs and Cs when you push them into the list, and throwing the rest
away; what's in the list is really just As, not Bs or Cs.
If you're storing A*s, on the other hand, you're in good shape. If you
call a virtual member function of A through an A* that really points to
a B or C, B or C's version will be called. Consult your friendly local
C++ book for discussion of 'virtual functions' and 'slicing'.
Or perhaps you mean that you're deriving classes without adding
variables, and you hope to store them all in one collection because you
have not increased the size? C++ doesn't provide a mechanism for doing
that; even if your B is the same size as A and would fit, it's a copy of
the base class that would go in the list. RTTI won't help you; it's up
to you to static_cast down based on some type variable or something
similarly unpleasant.
This doesn't belong in comp.std.c++; comp.lang.c++[.moderated] would've
been better.
--
Nate Lewis, MCSD
nlewis@mindspring.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Benjamin M. Stocks" <stocksb@execpc.com>
Date: 1998/09/23 Raw View
If I have a base class, call it 'A'. Then I derive class 'B' and class 'C'
from it. Now to store B and C in the same data structure I need to upcast
instances of B and C to class A and push them into a list. When I reference
them in the list, ex an iterator, is there anyway to tell the compiler to
use the derived member functions (from B and C) and not the base class
member functions or am I limited to the member functions of the class I used
to intialize the list?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Deb Bacon" <sendnomail@forgetit.com>
Date: 1998/09/23 Raw View
Does your compiler have RTTI (runtime type identification) support? If so,
look into it as you will be able to use it to branch your code to do a
dynamic_cast to the correct subclass. If not, then you could add an
attribute in your base class that identifies object's class and use it to
branch.
Benjamin M. Stocks wrote in message <6u9ddl$gbk@wiscnews.wiscnet.net>...
>If I have a base class, call it 'A'. Then I derive class 'B' and class 'C'
>from it. Now to store B and C in the same data structure I need to upcast
>instances of B and C to class A and push them into a list. When I reference
>them in the list, ex an iterator, is there anyway to tell the compiler to
>use the derived member functions (from B and C) and not the base class
>member functions or am I limited to the member functions of the class I
>used to intialize the list?
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]