gpt4 book ai didi

c++ - 用其他静态常量成员初始化静态常量成员

转载 作者:行者123 更新时间:2023-11-27 23:53:44 25 4
gpt4 key购买 nike

<分区>

我有 2 个带有静态常量成员的类,我希望它们是相同的,所以我使用其他类的其他静态常量成员,并且我想用公共(public)类的值初始化这些成员。它看起来像这样:A.h:

class A { 
public:
static const std::string a;
};

A.cpp:

const std::string A::a = "hello";

B.h:

class B {
public:
static const std::string b;
};

B.cpp:

const std::string B::b = A::a;

C.h:

class C {
public:
static const std::string c;
};

C.cpp:

const std::string C::c = A::a;

问题是我不能用其他静态成员初始化静态常量成员,因为静态成员是在程序运行开始时初始化的,并且在尝试初始化 B::b 时 A::a 的值不可用和 C::c 所以我得到了段错误。

我能做些什么来解决这个设计问题?

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