gpt4 book ai didi

c++ - 为一个 C++ vector 成员添加一个标志

转载 作者:行者123 更新时间:2023-11-28 05:28:45 25 4
gpt4 key购买 nike

旧程序使用 vector 作为自定义类型的容器,例如:

std::vector<my_type> m_my_type_queue;

它正在使用 vector 的 api,例如:

m_my_type_queue.size();
m_my_type_queue.begin();

现在我想用一个额外的标签扩展队列,所以我将 vector 和一个标签嵌入在一起,例如:

struct my_type_queue {
std::vector<my_type> m_vector;
mutable bool tag;
}

my_type_queue m_my_type_queue;

但是这个实现的问题是我需要用一个额外的层来访问队列,比如:

m_my_type_queue.m_vector.size();
m_my_type_queue.m_vector.begine();

我的问题是我如何可以像以前一样访问成员但也可以访问标记,例如:

m_my_type_queue.size();      // the same as original implementation
m_my_type_queue.begin(); // the same as original implementation
m_my_type_queue.tag = true; // new feacture with tag access

我可以为 vector 重新定义这些 API 并将它们转发给真正的 vector ,但是有什么简单的方法可以做到这一点吗?

最佳答案

满足您需求的一种方法是使您的 my_type_queue 派生自 std::vector。

但我会推荐一种不同的方式:按原样使用 vector,并维护一个单独的标志。

关于c++ - 为一个 C++ vector 成员添加一个标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40002495/

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