gpt4 book ai didi

c++ - 封装私有(private)枚举

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:12:30 26 4
gpt4 key购买 nike

之前我已经在类的头文件中定义了旨在私有(private)的枚举类型。

private:
enum foo { a, b, c };

但是,我不想再公开枚举的详细信息。在实现中定义枚举是否类似于定义类不变量?

const int ClassA::bar = 3;
enum ClassA::foo { a, b, c };

我想知道这是否是正确的语法。

最佳答案

C++ 没有枚举的前向声明,因此您不能将枚举“类型”与枚举“实现”分开。

在 C++0x 中以下是可能的:

// foo.h
class foo {
enum bar : int; // must specify base type
bar x; // can use the type itself, members still inaccessible
};

// foo.cpp
enum foo::bar : int { baz }; // specify members

关于c++ - 封装私有(private)枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1837159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com