help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: inheritance scope


From: Thomas Maeder
Subject: Re: inheritance scope
Date: Sat, 27 Nov 2004 10:43:47 +0100
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Security Through Obscurity, linux)

Gav <gav@cs.york.ac.uk> writes:

> #include <iostream>
> class A
> {  public: void foo() { std::cerr << "A::foo(int)" << std::endl; }
> };
> class C : public A
> {  public: void foo(int) { std::cerr << "C::foo(int)" << std::endl; }
> } c;
> int main()
> {  c.foo();

The name foo is looked up in an ordered set of scopes. Once it's found in
a scope, the subsequent scopes in the set are ignored, even if the occurences
in the first scope aren't applicable. This is called "name hiding".

I.e. foo is found to name a member function in class C. This means that
the compiler will not look for foo in class A. Either one of the foo's in
class C can be used, or the call can't be resolved, as in this case.


The typical solution is to add the version of foo() requiring no paramaters
to the interface of class C:

class C : public A
{
  public:
     void foo(int);

     using A::foo;
};

The member function foo() is now overloaded in class C.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]