C++ class 成员基数和声明之间的映射是否正确?

Is this mapping between C++ class member cardinality and declaration OK?

我在设计中为 class(structure) 个成员设置了三种基数。

我已将它们映射到以下声明中作为 class(结构) 成员

class Foo {
        ExactlyOnce exactlyOnce;
        std::unique_ptr<ZeroOrOnce> zeroOrOnce;
        std::list<std::shared_ptr<ZeroOrMore>> zeroOrMore;
};

我打算在整个程序包中推广这种模式,这种方法是否可行,或者它有一些错误 w.r.t。成员基数?

好像还不错

尽管考虑以下几点:

  • ZeroOrOne 使用 boost::optional(如果您已经使用 boost);它背后的意图更加明确,因为 API 为此进行了优化。

  • ZeroOrMore 使用 std::vector<T> 而不是 std::list<std::shared_ptr<T>>。在你有 large/expensive 个对象并执行前面和随机插入的情况之外,向量往往更有效。

对于 zeroOrMore 基数按值而不是指针存储元素。存储指针仅作为最后的手段,如果别无他法。