I already read this Same name structure with different definition in C But it doesn't clear my question. Wouldn't it be better to separate declaration and definition like function? Because it is possible, following code can work.
//AAA.c
class AAA
{
public:
// even if AAA only have 1 member variable, it works.
char i;
char f;
char d;
char e;
void PrintAYAYA();
};
void AAA::PrintAYAYA() { std::cout << "AYAYA\n"; };
void GetAAAWhatHappen(AAA a)
{
char* p = (char*)&(a.i);
char arr[4]{};
for (int i = 0; i < 4; ++i)
std::cout << (int)*(p + i) << std::endl;
}
//main.c
class AAA
{
public:
int i = 0x12345678;
void PrintAYAYA();
};
void GetAAAWhatHappen(AAA a);
int main()
{
AAA a;
a.PrintAYAYA();
GetAAAWhatHappen(a);
}
Despite being distinctly different definitions, the above code runs because they share the same name
And another question, afer the compiler links the object files and create an exe file, when execute exe file, how does computer distinguish classes with the same name but different definitions?