C++存储全局 std::字符串标签与 getter 和 setter 的策略
C++ strategy for storing global std::string label with getter and setter
在我正在从事的一个C++项目中,我需要跟踪一个标签。标签只是存储附加到写入各种文件的结果的std::string
,以便结果可以映射到已实现算法的特定阶段。
以下是跟踪标签的机制的要求:
- 所有翻译单元都需要访问此标签
- 标签必须能够在运行时修改
- 需要通过吸气/设置器功能控制对标签的访问
- 始终需要正好 1 个标签
这并不难实现。 但是,尽管如此,我在这里提出这个问题,因为我怀疑这是通常---做的事情,或者至少与通常做的事情非常相似。
我能想到的最好的解决方案是有一个如下类,然后在任何地方都包含接口:
class Label {
public:
static std::string get();
static int set(std::string s);
private:
static std::string label;
};
std::string Label::get() { return label; }
int Label::set(std::string s) {
if( /* OK to change to "s" */ ) {
label = s;
return 0;
}
return 1;
}
std::string Label::label = "";
因为这些标签中总是只有 1 个,所以似乎应该有比创建类更好的解决方案。 有什么建议吗?
我倾向于怀疑更广泛的类是否可能没有更多用途,例如:
template <class T>
class cond_write {
T val;
std::function<bool()> c;
public:
template <class Cond>
cond_write(T const &t, Cond c): val(t), c(c) {}
cond_write &operator=(T const &t) {
if (c())
val=t;
return *this;
}
operator T() const { return val; }
};
然后,您将使用(在您的情况下)std::string
和 lambda 来实例化它,以表示可以发生写入的条件。
而不是get
和set
,你只需分配给它,或将其用作T(在你的例子中是std::string
)。例如:
cond_write<std::string> label("Initial label", []() { return whatever(); });
// equivalent to label.set("This might be the new label");
label="This might be the new label";
// equivalent to std::string new_label = label.get();
std::string new_label=label;
我认为这里不需要类,并建议使用自由函数和命名空间。您具有相同的范围语义,但没有所有装饰,例如static
。它们还允许您保持内部结构的私密性,就像您在类中一样。一些小的更改,您的代码最终如下所示。
页眉
namespace Label
{
std::string get();
// only require a copy when necessary and allow rvalues.
int set(const std::string& s);
};
实现
namespace // unnamed namespace is generally recommended over using static
{
std::string label;
}
std::string Label::get() { return label; }
int Label::set(const std::string& s)
{
if( /* OK to change to "s" */ )
{
label = s;
return 0;
}
return 1;
}
相关文章:
- 如何为 C 型字符串数组编写 getter 和 setter?
- 有没有办法在没有括号的情况下在C++中调用成员的getter/setter?
- Boost python getter/setter with the same name
- 如何为数组成员变量声明 getter/setter
- 具有多行字符串宏观的Getter/Setter生成
- 专用对象"getter setter"特定对象
- 具有相同名称的Getter/Setter
- C++ 中 getter setter 和构造函数之间的视觉差异
- C++中的getter/setter是正确的方法
- 绑定C++成员变量:getter/setter 或原始指针
- C++getter/setter、互斥、细粒度锁定
- Getter & Setter 的最佳性能
- 如何在公共 API 接口类中将 auto getter&setter 与 PIMPL 设计模式相结合
- 混合getter /setter和普通变量
- 如何重构冗余的getter/setter方法
- 如何在托管c++中使用手写的getter/setter定义属性
- C++ getter/setter paradigm
- C++ Getter/Setter (Alternatives?)
- 使用值包装器和 operator() 重载来简化 getter/setter 设计:这是一种危险的做法
- c++中堆对象getter / setter的更好实践