Question about inheritance

Alexandre Oliva oliva@dcc.unicamp.br
Mon Aug 3 23:59:00 GMT 1998


Peter V Evans <pevans@enteract.com> writes:

> class foo { public: typedef int hi;
> 	void doit (void) { if (sizeof(hi) == sizeof(int))
[snip]
> class stuff : public foo { public: typedef double hi; };

> The function doit, thus, does not see the derived class' "hi", only the
> parent class.

That's right, sizeof(hi) is evaluated at compile-time, within the
scope of class foo.  There's no such thing as overriding of type
declarations in subclasses.  The closest you can get to this is to
define a virtual member function that returns sizeof(foo::hi), and
override it in the derived class so that it returns sizeof(stuff::hi)

> a better question would be, how to get doit to somehow see the
> derived class "hi" instead of the parent class "hi"?

I can't think of any clean way to do that in the base class :-(

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil




More information about the Gcc mailing list