TITLE: scopes and name hiding (Newsgroups: comp.lang.c++.moderated) PROBLEM: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner) Should the following rather artificial piece of code compile ? struct A {}; struct B { B(const A&); }; struct C : B { C() : B(A()) {} int B; }; RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 24 Apr 96 Name lookup for member-init-lists and the bodies of member functions is performed as if the function definition occurred following the complete class definition. So the meaning of C::C() is the same if we write it all out like this: struct C : B { C(); int B; }; C::C() : B(A()) { } The name of int member B hides the name of the base class in the scope of C. Type names and object names can coexist in the same scope, but can hide one another if they are in different scopes. Thus, the initializer B(...) must refer to int member B. In this example, there is no conversion from A to int, so the code is ill-formed. If we add an operator int() to class A, int member B could then be initialized with an A. But base class B would need to be initialized with a default constructor, and there isn't one, so the code is still ill-formed. Finally, if we add a default constructor to B, the code becomes well-formed as far as the C++ language definition is concerned. The programming style, however, would serve as an example to be avoided. :-)