C ++: Does a class inherit from a class into a namespace?

Here are some contrived code examples:

template<typename T> void Do(T arg) { (void)arg->b; } namespace A { struct Foo { int a; }; } namespace B { struct Foo { int b; }; struct Bar : A::Foo { void Blah() { Do((Foo *)0); } }; } 

What when compiling with gcc 4.8.2 (clang gives a similar error):

 namespacebug.cpp: In instantiation of 'void Do(T) [with T = A::Foo*]': namespacebug.cpp:10:34: required from here namespacebug.cpp:1:39: error: 'struct A::Foo' has no member named 'b' template<typename T> void Do(T arg) { (void)arg->b; } ^ 

Note that in the error this applies to T = A::Foo , although on the call site I create Foo in the namespace B If I remove the base class decl ( : A::Foo ), then everything compiles fine.

Does this seem to suggest that inheriting from A::Foo somehow brings it into my namespace and maps it to my use of Foo ? What is a C ++ "function"?

(Of course, this problem can be easily fixed with the namespacing of my use of Foo , but that is not a question.)

+7
c ++ namespaces
source share
2 answers

Due to the rule with the class entered, the class name is displayed as if it were a member.

9/2

The class name is inserted into the area in which it is declared immediately after viewing the class name. The class name is also inserted into the scope of the class itself; this is known as the name of the introduced class. To verify access, the name of the entered class is considered as the name of the public member.

So, it is as if the class A::Foo contains a member Foo , which calls the type A::Foo . Because a name search in Bar::Blah() looks up the base members of Bar before the namespace members, a name search for Foo finds the name of the entered class, which calls A::Foo .

+5
source share

Does the class inherit from the class into the namespace?

Grade. If the name search is not performed in the class, then the search continues in the base class.

10.2 Search for username

...

5 Otherwise (that is, C does not contain the declaration f or the result set of declarations is empty), S(f,C) initially empty. If C has base classes, compute the search set for f in each subobject of the direct base class Bi and combine each such search set S(f,Bi) , in turn, into S(f,C) .

Searching for a username includes searching for nested types. Later in the same section we find:

9 [Note. A static member, nested type, or enumerator defined in the base class T can be unambiguously found even if the object has more than one subobject of the base class of type T. Two subobjects of the base class share non-static member subobjects of their common virtual base classes. -end note]

+3
source share

All Articles