How can a compiler distinguish between static data members having the same name in different classes in C ++?

I recently had an interview in C ++ where I was asked how the compiler distinguishes static data members with the same name in two different classes?

Since all static data variables are stored in the data segment, there must be a way in which the compiler keeps track of which static data belongs to the class, especially when they have the same name.

Edit: I answered the name manufactory, but he refused to say that name manipulation is used only among members of the same class.

+5
source share
8 answers

Names are distorted with their class name in them. Clang compiler example

class A {
  static int i;
};

int A::i = 0;

$ clang++ -cc1 -emit-llvm main1.cpp -o -
; ModuleID = 'main1.cpp'
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
target triple = "i386-pc-linux-gnu"

@_ZN1A1iE = global i32 0, align 4

_ZN1A1iE

$ c++filt _ZN1A1iE
A::i
+12
+6

, , , , . , , :

class one
{
   static int data;
};

class two
{
   static int data;
};

one::data
two::data

. .

+2

- , , .

, , ( ), , , , :)

-.

+2

"" , , , .

.

{*}. . , ( ).

{*} , RTTI.

+2

: myclass::mymemb. , , .

mangling; , __i_7myclass_6mymemb.

The process of extracting a name (or its necessity) is the reason that we need it extern "C"for compatibility with C-interfaces that define names without distortion.

+1
source

static class members will have their names distorted in such a way that the class name is part of the "global" name that the linker sees.

0
source

It appends the class name to the data member name. (And then the name-distorts it all.)

0
source

All Articles