pure virtual functions and name injection

Pierre Chatelier ktd@club-internet.fr
Mon Mar 6 21:02:00 GMT 2006


Hello,

I have posted a message in the gcc mailing list, but it appears that  
what I thought that was a bug is in fact a misunderstanding of mine.
Here is the message:
http://gcc.gnu.org/ml/gcc/2006-03/msg00195.html

It can be summarized in "the following code does not compile"

//-------------------------------------
struct a
{
   virtual int foo() =0;
   virtual ~a(){}
};

struct b : public a
{
   virtual int foo(int a) =0;
   virtual ~b(){}
};

struct c : public b
{
int test()
{
return (foo() + // <--- the compiler claims here that it cannot find  
foo()
foo(2));
}
virtual ~c(){}
};

struct d : public c
{
   virtual int foo() {return 1;}
   virtual int foo(int a) {return a;}
   virtual ~d(){}
};

int main()
{
   d call;
   return call.test();
}
//-------------------------------------

The answer is  http://gcc.gnu.org/ml/gcc/2006-03/msg00196.html

> This is not a bug in gcc.  foo in b hides the one from a.
> You can "fix" the source by:
> struct b : public a
> {
>   virtual int foo(int a) =0;
>   using a::foo;
>   virtual ~b(){}
> };
>
> Which interjects foo from a into b's "namespace".

That's ok, but I do not understand what's going on. Why should I  
inject the "foo" name from a into b ? The two functions have  
different prototypes, I thought that the compiler was able to  
differentiate them.

Here is a follow-up to that problem:
if I *do not* use "using  a::foo;" but rather replaces
	return (foo() + foo(2));
by
	return (a::foo() + foo(2));
Then it compiles but there is a *link* error !
Can somebody explain me what happens ?

Thanks in advance,

Pierre Chatelier



More information about the Gcc-help mailing list