小编chr*_*451的帖子

在C++中,当我需要使用枚举时,如何避免#including头文件?

在我的C++头文件中,我尝试使用前向声明(类MyClass;)而不是#including类头,正如许多C++编码标准(Google C++样式指南中的一个)所推荐的那样.

不幸的是,当我介绍枚举时,我不能再做前瞻声明了.像这样:

//// myclass1.hpp ////

class MyClass1
{
    enum MyEnum1
    {
        Enum_A, Enum_B, Enum_C
    };
};

//// myclass2.hpp ////

// I want to avoid this
#include "myclass1.hpp"

// I'd prefer to do this (forward declaration)
class MyClass1;

class MyClass2
{
    // This is o.k.: I only need to forward declare MyClass1
    MyClass1* ptr;

    // This forces me to #include, but I don't want to!
    void func( MyClass1::MyEnum1 e );
};
Run Code Online (Sandbox Code Playgroud)

到目前为止,我能想到的最好的解决方案是用成员常量替换枚举:

//// myclass1.hpp  ////

MyClass1
{
    static const int …
Run Code Online (Sandbox Code Playgroud)

c++ dependencies header include

5
推荐指数
2
解决办法
2616
查看次数

是否有任何C++工具可以检测到static_cast,dynamic_cast和reinterpret_cast的误用?

以下问题的答案描述的推荐用法static_cast,dynamic_cast以及reinterpret_cast在C++:

什么时候应该使用static_cast,dynamic_cast,const_cast和reinterpret_cast?

你知道有哪些工具可以用来检测这类演员的滥用吗?像PC-Lint或Coverity Static Analysis这样的静态分析工具会这样做吗?

提示这个问题的特殊情况是不适当地使用static_cast向下转换指针,编译器没有警告.我想用工具检测这种情况,而不是假设开发人员永远不会犯这个错误.

c++ static-analysis casting dynamic-analysis downcast

3
推荐指数
1
解决办法
1142
查看次数