This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Question about inheritance
- To: "Peter V. Evans" <pevans at enteract dot com>
- Subject: Re: Question about inheritance
- From: Ben Scherrey <scherrey at gte dot net>
- Date: Tue, 04 Aug 1998 04:04:21 -0400
- CC: egcs at cygnus dot com
- Organization: Proteus Technologies, Inc.
- References: <Pine.BSF.3.96.980803155026.21336A-100000@adam.enteract.com>
- Reply-To: scherrey at proteus-tech dot com
This is a C++ language issue and not related to the egcs compiler
implementation. Suffice to say, the scope of doit is strictly within
foo. You have two typedefs, specifically foo::hi and stuff::hi. Their
only similarities are the coincidence of their names which, as a
typedef, means nothing in a strongly typed language. To get the result
you request (but probably not what you want) try the following:
struct foo
{
typedef int hi;
virtual size_t hi_size( void ) const { return sizeof( hi ) };
void doit( void ) const
{ // What if you do sizeof( void* ), bool, et al ?
// This will usually say "int" on most platforms!
if( hi_size() == sizeof( int ) )
puts("int");
else
puts("something else");
}
};
struct stuff : public foo
{
typedef float hi;
virtual size_t hi_size( void ) const { return sizeof( hi ) };
};
I think you're barking up the wrong tree here...
Ben Scherrey
Peter V. Evans wrote:
>
> Hi,
>
> I'm developing a program which makes heavy usage of inheritance. Let me
> give the example first:
>
> class foo
> {
> public:
> typedef int hi;
>
> void doit (void)
> {
> if (sizeof(hi) == sizeof(int))
> puts("int");
> else
> puts("something else");
> }
> };
>
> class stuff : public foo
> {
> public:
> typedef double hi;
> };
>
> int
> main (void)
> {
> stuff s;
>
> s.doit(); // Prints out "int", not "something else"
> return 0;
> }
>
> ---
>
> The function doit, thus, does not see the derived class' "hi", only the
> parent class. I couldn't say if this is the correct behaviour, since I
> don't know C++ as well as most others, but a better question would be,
> how to get doit to somehow see the derived class "hi" instead of the
> parent class "hi"?
>
> Thanks for any help you can provide.
> Peter