Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does C++ syntax “A::B:A {};” mean

What does C++ syntax struct A::B:A {}; mean? Where is this name definition (or access) described in the C++ standard?

#include <iostream>  struct B;  struct A {     struct B; };  struct A::B:A { };  int main() {     A::B::A::B b;     std::cout<<"Sizeof A::B::A::B is " << sizeof(A::B::A::B)<<std::endl;     return 0; } 
like image 997
devsh Avatar asked Nov 29 '17 06:11

devsh


People also ask

What does :: mean in C programming?

:: is the scope resolution operator - used to qualify names.

What is the meaning of A&B in C language?

Arithmetic OperatorsAdds two operands. A + B = 30. − Subtracts second operand from the first.

What is %d and &N in C?

"%s%d%s%d\n" is the format string; it tells the printf function how to format and display the output. Anything in the format string that doesn't have a % immediately in front of it is displayed as is. %s and %d are conversion specifiers; they tell printf how to interpret the remaining arguments.

What is meant by a b in C++?

C++ Arithmetic Operators a + b; Here, the + operator is used to add two variables a and b . Similarly there are various other arithmetic operators in C++. Operator. Operation.


1 Answers

This definition

struct A {     struct B; }; 

Defines a struct A with a declaration of a nested struct B1. The fully qualified name of B is A::B, you could say B is inside the "namespace" of A. Then this:

struct A::B : A { // Note I added spaces }; 

Is the definition of A::B, and the single : specifies that it is derived from A.

Now, the interesting part is A::B::A::B. Let's dissect it:

  1. A::B names the nested structure.
  2. A::B::A accesses the injected class name A inside B. The injection is due to the inheritance.
  3. A::B::A::B names the nested structure B in A again.

And you can continue ad-infinitum, or at least until your compiler meets its translation limit2.

A fun intellectual exercise, but avoid like the plague in actual code.


[class.qual]/1 explains how the lookup works

If the nested-name-specifier of a qualified-id nominates a class, the name specified after the nested-name-specifier is looked up in the scope of the class ([class.member.lookup]), except for the cases listed below. The name shall represent one or more members of that class or of one of its base classes (Clause [class.derived]).

And the text above allows us to name the base class because [class]/2

The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name.

The above clearly says that starting a fully qualified name with A:: allows you to specify a member or a base class. Since A has no bases, you can only specify A::B (a "member type"). But A::B also nominates a class. So we may specify a base or member of that as well with A::B::, which allows us to name A::B::A. Now rinse and repeat.


1 - Note it's a completely other B. Not at all related to the global struct B.
2 - A recommended minimum of 256 according to [implimits]/2.36

like image 108
StoryTeller - Unslander Monica Avatar answered Sep 28 '22 00:09

StoryTeller - Unslander Monica