This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Question about inheritance


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



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]