TITLE: name hiding fundamentals (Newsgroups: comp.lang.c++.moderated, 16 Jun 99) UPCHURCH: Tony Upchurch >> I have been studying virtual functions, and the >> textbook I have suggests that declaring a function >> virtual will allow a derived class function to override >> the base class function of the same name and >> signature. However, I can get this kind of behavior >> without declaring the function virtual. Is there something >> I am missing here? CHANDRASHEKHAR: Chandrashekhar > class base > { > void func(int); > void func(int, int); > }; > > class derived: public base > { > void func(int); > }; > > If you dont declare func as virtual in class base, base:func(int, int) > will be hidden in derived. CLAMAGE: Steve Clamage Sorry, that is not correct. Function base::func(int,int) is always hidden in derived, whether or not any of the functions are declared virtual. In fact, base::func(int) is also hidden in derived, whether or not it is virtual in base. Name hiding via scopes is fundamental in C++, as in all block- structured languages with which I am familiar. The basic rule of scopes is that declaration of a name in an inner scope hides all instances of that name in all outer scopes. In C++, a base class is considered to form a scope enclosing the scope of all of its derived classes. No matter whether any of the functions are virtual, given derived *pd; derived d; the expressions pd->func(1) d.func(1) never call base::func. There is no way to refer to base::func via pd or d without using a scope qualifier, as in d.base::func(1) The expressions pd->func(1,2) d.func(1,2) are invalid, because the func in derived hides all the funcs in base. Name hiding is unrelated to the concept of virtual functions in C++, whose purpose is to provide polymorphic behavior. TRIBBLE: David R Tribble It was probably an unintentional omission, but without a 'public:' in front of the member function declarations, all the base::func() functions are hidden/inaccessible from class derived, and everyone else, too.