从另一个模板类访问实例变量 [英] accessing instance variable from another template class

查看:162
本文介绍了从另一个模板类访问实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(主要是从从其他类模板访问变量粘贴的)分隔两个问题)



我试图创建一个可以与数据加载器类一起使用的容器类系统从文本文件加载数据



这里是两类数据:

  class Customer 
{
// ...
};

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

这是我的两个容器类:

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

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

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

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

// ...
};

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



容器类按顺序处理插入,数据加载器管理列表并创建



这是我的问题:



我加载我的客户文件在第一,并填充该列表。



之后,我必须加载的旅游,其中有客户的客户预订他们,我想将这些客户存储在每个旅游对象的指针向量中,使得客户信息容易访问。



在我将customerID存储为列表字符串,那么当游览都被加载时,将custList传递到一个函数中,该函数通过custList搜索,匹配字符串列表



这意味着我必须维护两个列表,字符串和其他指针之一,基本上双处理所有的数据..考虑到数据集是相当大的,这意味着加载时间更长的时间..



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



技术上一切都发生在DataLoader类的范围内,所以我认为应该是可能的,但我只是不太确定如何去。它可能使它成为一个朋友类?我已经尝试这样做,但迄今为止有任何运气,到目前为止..



任何帮助将非常感谢,对不起长时间解释,希望它有意义。

解决方案

最终的流使用方式如下:

  custStream>>客户>> toursStream>>旅游为了实现这一点,你必须用ifstream包装两个流 - 为客户和旅游,并保持客户列表中,这里是代码,你可以替换容器到你最喜欢的:

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

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

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

CustomersInFileStream& operator>>(CustomersInFileStream& input,std :: vector< Customer> customers){
//此处使用ifstream成员
输入执行文件解析。客户=客户;
return input;
}

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

ToursInFileStream& operator>>(ToursInFileStream& input,std :: vector< Tour> tours){
//此处使用ifstream成员
//你也有客户列在这里
return input;
}

int main()
{

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

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

std :: vector< Customer>顾客;
std :: vector< Tour>旅游

custStream>>客户>> toursStream>>旅游

return 0;
}


(mostly pasted from accessing variable from another class template to separate two problems)

i am trying to make a system of container classes that can be used with a data loader class to load data from text files

here are the two classes of data:

class Customer
{
    //...
};

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

these are my two container classes:

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

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

and finally my data loader template:

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

    //...
};

i have overloaded the >> operator in Customer and Tour so that an ifstream can be passed to them, a line is taken from the stream, tokenised and put it into the object instance variables.

the container classes handle the insertion in order and the data loader manages the lists and creates the ifstream so that it can be passed to the objects.

this is my problem:

i am loading my customers file in first, and populating that list.

after that i have to load in the tours, which have customerIDs for the customers that booked them, and i want to store those customers in a vector of pointers in each of the tour objects so that the customer information is easily accessible.

at the moment i am storing the customerIDs as a list of strings, then when the tours are all loaded, passing custList into a function that searches through custList, matching it with the list of strings

this means i am having to maintain two lists, one of strings and the other pointers, and basically double handle all the data.. considering the datasets are quite large, this means a lot longer in loading time..

so i was wondering if there is a way that i can access the custList instance variable from inside the overloaded >> operator for Tour and generate the list of pointers as i create the Tour objects?

technically everything is occurring inside the scope of the DataLoader class, so i think it should be possible, but im just not too sure how to go about it.. maybe make it a friend class? i have tried doing that but havent had any luck so far..

any help would be greatly appreciated, and sorry for the long winded explanation, hopefully it made sense..

解决方案

The final stream usage can look like this:

custStream >> customers >> toursStream >> tours;

To achieve this, you must wrap ifstream with two streams - for customers and for tours and keep the customer list in the stream, here's the code, you can replace the container to your favorite:

#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;
}

这篇关于从另一个模板类访问实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆