恐怖的C++之--class name的作用域和sizeof

9.1 Class names

2. A class definition introduces the class name into the scope where it is defined and hides any class, object,function, or other declaration of that name in an enclosing scope (3.3). If a class name is declared in ascope where an object, function, or enumerator of the same name is also declared, then when both declarationsare in scope, the class can be referred to only using an elaborated-type-specifier (3.4.4).

 

代码
   
     
struct aa{ int k[ 20 ];};
void * bb = NULL;
int main(){
void * aa = NULL;
struct bb{ int k[ 20 ];};
struct cc{ int k[ 20 ];};
void * cc = NULL;
void * dd = NULL;
struct dd{ int k[ 20 ];};
printf(
" %u %u %u %u " , sizeof (aa), sizeof (bb), sizeof (cc), sizeof (dd));
return 1 ;
}

所以这段代码的输出能够确定为4 80 4 4

 

如果想避免混淆,也可以这样做

如果需要T是个type的话,sizeof(const T)

如果需要V是个变量,或者表达式的话,sizeof((V));

这样编译器会做检查。

 

总结,C++真的很蛋疼。

你可能感兴趣的:(sizeof)