zoukankan      html  css  js  c++  java
  • *的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++真的很蛋疼。

  • 相关阅读:
    07组合,模版
    06享元、责任链
    05观察,命令
    04代理,迭代器
    03单例,策略
    02工厂,创建者
    01基础
    css随记02布局
    css随记01编辑技巧,背景与边框
    nodejs随记03
  • 原文地址:https://www.cnblogs.com/aoaoblogs/p/1806724.html
Copyright © 2011-2022 走看看