help-gplusplus
[Top][All Lists]
Advanced

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

Re: inheritance scope


From: Maurizio Loreti
Subject: Re: inheritance scope
Date: 26 Nov 2004 21:08:51 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

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

> hi,
> 
> could someone tell me why this program (which results in a compilation
> error) shouldn't compile (i.e. is it a gcc thing or is it a strict part of
> c++), and how i can make it compile without having to put C:: before foo()
> in main()?
> 
> essentially i want the foo() from A to be overloaded as i would expect if it
> was actually inserted into C.
> 
> thanks, gav
> 
> #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();
>    return 0;
> }

This is a FAQ...  change c.foo to c.A:foo().

The reason is the language Standard...  as explained in the C++
template FAQ
(http://www.decadentplace.org.uk/womble/c++/template-faq.html): 

"Q:  My compiler says that a member of a base class template is not
defined in a derived class template. Why is it not inherited?"
... example like your deleted...
"A: It is inherited. However, the standard says that unqualified names
in a template are generally non-dependent and must be looked up when
the template is defined. Since the definition of a dependent base
class is not known at that time (there may be specialisations of the
base class template that have not yet been seen), unqualified names
are never resolved to members of the dependent base class. Where names
in the template are supposed to refer to base class members or to
indirect base classes, they can either be made dependent by qualifying
them or brought into the template's scope with a using-declaration. In
the example, this could be achieved by replacing the call to
base_func() with this->base_func() or base<T>::base_func(), or by
adding the declaration using base<T>::base_func;."

-- 
Maurizio Loreti                         http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy              ROT13: ybergv@cq.vasa.vg


reply via email to

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