gpt4 book ai didi

c++ - 将静态常量数据添加到已定义的结构中

转载 作者:太空狗 更新时间:2023-10-29 21:08:31 27 4
gpt4 key购买 nike

由于类中的静态常量数据实际上只是常量的 namespace 糖,我认为

struct A {
float a;

struct B {
static const int b = 2;
};

};

相当于

struct A {
float a;
};

struct A::B {
static const int b = 2;
};

或类似的东西。在 C++ 中可能有这样的事情吗?如果能够使用此类信息标记我从第三方库中提取的类定义,这对我很有用。

最佳答案

您无法在 C++ 中重新打开结构/类定义,因此您能做的最好的事情就是创建第三方结构的派生版本并以这种方式添加您的常量:

struct My_A : public A 
{
static const int b = a;
};

否则,您可以使用基于 struct typeid 的键维护常量映射。

我也很喜欢 Georg 的想法。

关于c++ - 将静态常量数据添加到已定义的结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2845539/

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