C++ 正在聚合的结构必须知道来自聚合此结构的结构的信息

C++ 正在聚合的结构必须知道来自聚合此结构的结构的信息,c++,c++11,vector,stl,C++,C++11,Vector,Stl,就像在std::pairvector必须知道它的pair-first元素(即int) 我得到了以下有趣的问题。提供代码,请阅读注释 #include <vector> #include <string> #include <iostream> namespace DataWorld { using DataType = int; // It is a data. Holder struct will hold this data. But

就像在std::pairvector必须知道它的pair-first元素(即int)

我得到了以下有趣的问题。提供代码,请阅读注释

#include <vector>
#include <string>
#include <iostream>

namespace DataWorld {
    using DataType = int;

    // It is a data. Holder struct will hold this data. But holder has it's id
    struct Data {
        DataType storage[5];
        void Show() {
            std::cout << "my_holder_index" << " "; // here the data has to know the id of the holder that holds this data
            for(auto elem : storage) {
                std::cout << elem << " ";
            }
            std::cout << std::endl;
        }

    };
    struct DataHolder {
        Data my_data;
        std::string my_holder_index;
        DataHolder(Data my_data, std::string my_holder_index)
            : my_data(my_data)
            , my_holder_index(my_holder_index) {}
        void Show() {
            // Do not show holder id here! It is the data that has to know it.
            my_data.Show();
        }
    };
}

int main() {
    using namespace DataWorld;

    DataHolder dhs[] = {
        { {1, 2, 3, 4, 5}, "nat" },
        { {1, 3, 5, 7, 9}, "even" },
        { {2, 4, 6, 8, 10}, "odd" }
    };

    for(auto dh : dhs) {
        dh.Show();
    }
}
#包括
#包括
#包括
命名空间数据世界{
使用DataType=int;
//它是一个数据。Holder结构将保存此数据。但Holder有它的id
结构数据{
数据类型存储[5];
无效显示(){

std::cout既然你还没有解释你的问题中的任何东西与
向量
有什么关系(还提出了一个荒谬的说法,即
对中的
向量
可以访问
对中的另一个元素,我将集中精力为你给出的实际代码找到一个解决方案

问题2:

std::vector是否可能在内存中找到其DataHolder结构实例化的位置,并尝试查找我的\u holder\u索引

不存在C++中的合法方法(即:不调用UB),以便成员UBBBIKET获得对其拥有对象中声明的变量的访问权。至少,没有将变量传递给它。< /P> 哦,当然,您可能可以将
这个
转换为一个
无符号字符*
,执行一些指针运算,然后将其转换为
std::string
。这可能会“起作用”对于某些实现,但它不是合法的C++。如果没有其他理由,在没有提供其他代码的地方(没有提供代码< > STD::String < /Cuth>成员> /P>,没有任何东西阻止您声明<代码>数据<代码>。 问题1和3:

可以解决此任务,而无需手动为数据提供“my_holder_index”变量(例如:my_Data(my_Data(my_holder_index)))

是否有可能在没有点内存操作的情况下使用C++ +STL实现这个行为? 正确的方法是使用CRTP。

数据
应该是
数据持有者
的基类子对象(或者任何其他人可能想要使用它)。它将获取将从其派生的类作为模板参数。因此,它可以
静态\u将此
转换为指向派生类的指针。只要始终将派生类作为模板参数传递,就不会引发未定义的行为:

template<typename Derived>
  //Requires that `Derived` have a publicly accessible `my_holder_index` field
  //Which is something that has an `operator<<(ostream)` overload for it.
struct Data {
    DataType storage[5];
    void Show() {
        std::cout << "my_holder_index" << derived().my_holder_index;
        for(auto elem : storage) {
            std::cout << elem << " ";
        }
        std::cout << std::endl;
    }

private:
    Derived &derived() {return static_cast<Derived&>(*this);}
    const Derived &derived() const {return static_cast<const Derived&>(*this);}

};
struct DataHolder : Data<DataHolder> {
    std::string my_holder_index;
    DataHolder(std::string my_holder_index)
        : my_holder_index(my_holder_index) {}
    void Show() {
        // Do not show holder id here! It is the data that has to know it.
        Data<DataHolder>::Show();
    }
};
模板
//要求“派生”具有可公开访问的“我的持有者索引”字段

//这是一个有“运算符”的东西没有,没有,没有,这听起来像是一个X/Y问题。
std::vector
和什么都有关系吗?你一直在谈论它,但是你的代码没有在任何地方使用它。你需要
DataHolder
做什么?除了
stor之外,为什么
Data
不能按住
my_holder\u index
年龄?“就像在std::pairvector必须知道它是pair-first元素(即int)。“嗯,不,不知道。
避免您描述的情况的任务是
,但是为什么要避免这种情况?练习的最终目标是什么?缺乏对后者的解释使它成为一个问题