c++ - 从另一个模板类访问实例变量

标签 c++ instance-variables friend-function

(主要是从 accessing variable from another class template 粘贴来区分两个问题)

我正在尝试制作一个容器类系统,可以与数据加载器类一起使用以从文本文件加载数据

这是两类数据:

class Customer
{
    //...
};

class Tour
{
    std::vector<Customer*> custPtrs;
    //...
};

这是我的两个容器类:

template <class T>
class P_VContainer
{
    boost::ptr_vector<T> data;
    //...
};

template <class T>
class ListContainer
{
    std::list<T> data;
    //...
};

最后是我的数据加载器模板:

template<template<class> class T>
class DataLoader
{
    T<Customer> custList;
    T<Tour> tourList;

    //...
};

我在 Customer 和 Tour 中重载了 >> 运算符,以便可以将 ifstream 传递给它们,从流中取出一行,标记化并将其放入对象实例变量中。

容器类按顺序处理插入,数据加载器管理列表并创建 ifstream,以便它可以传递给对象。

这是我的问题:

我首先加载我的客户文件,然后填充该列表。

之后,我必须加载旅游,其中包含预订客户的客户 ID,我想将这些客户存储在每个旅游对象的指针 vector 中,以便轻松访问客户信息。

目前我将客户 ID 存储为字符串列表,然后当所有游览都加载后,将 custList 传递到一个函数中,该函数搜索 custList,将其与字符串列表匹配

这意味着我必须维护两个列表,一个是字符串,另一个是指针,并且基本上双重处理所有数据。考虑到数据集非常大,这意味着加载时间要长得多。

所以我想知道是否有一种方法可以从 Tour 的重载 >> 运算符内部访问 custList 实例变量并在创建 Tour 对象时生成指针列表?

从技术上讲,一切都发生在 DataLoader 类的范围内,所以我认为这应该是可能的,但我不太确定如何去做......也许让它成为一个友元类?我试过这样做,但到目前为止还没有运气......

任何帮助将不胜感激,对于冗长的解释,我们深表歉意,希望它是有道理的..

最佳答案

最终的流用法可能如下所示:

custStream >> customers >> toursStream >> tours;

要实现这一点,您必须用两个流包装 ifstream - 用于客户和旅游,并将客户列表保留在流中,这是代码,您可以将容器替换为您喜欢的:

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

class CustomersInFileStream {
public:
    std::vector<Customer> customers;
    std::ifstream &input;
    CustomersInFileStream(std::ifstream &fileIn)
        :input(fileIn){
    }
};

class ToursInFileStream {
public:
    std::vector<Customer> customers;
    std::ifstream &input;
    ToursInFileStream(std::ifstream &fileIn)
        :input(fileIn){
    }
};

CustomersInFileStream &operator>>(CustomersInFileStream &input, std::vector<Customer> customers) {
    // perform file parsing here using ifstream member
    input.customers = customers;
    return input;
}

ToursInFileStream &operator>>(CustomersInFileStream &customersStream,
                                  ToursInFileStream &toursStream) {
    toursStream.customers = customersStream.customers;
    return toursStream;
}

ToursInFileStream &operator>>(ToursInFileStream &input, std::vector<Tour> tours) {
    // perform file parsing here using ifstream member
    // you also do have customers list here
    return input;
}

int main()
{

    std::ifstream customersFile("~/customers.txt");
    std::ifstream toursFile("~/tours.txt");

    CustomersInFileStream custStream(customersFile);
    ToursInFileStream toursStream(toursFile);

    std::vector<Customer> customers;
    std::vector<Tour> tours;

    custStream >> customers >> toursStream >> tours;

    return 0;
}

关于c++ - 从另一个模板类访问实例变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19469878/

相关文章:

c++ - 使用 C++ 元编程构建静态字符串

c++ - select() 总是返回 0

c++ - 如何正确实现与基类不同的版本?

python tkinter在函数之间传递变量

c++ - 它是 C++ gcc HEAD 10.0.0 20190 相对于友元函数的错误吗

C++ 继承 : confused with base member access from derived object

controller - 导轨 5 : Local variables in form partial

ruby-on-rails - ActiveRecord:更改并保存模型内的对象状态

c++ - 是否可以有一个只能通过 ADL 找到的非友元函数?

c++ - 我正在尝试编写用于在 C++ 中添加 2 个复数的友元函数