2013-10-19 49 views
0

(主要來自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; 

    //... 
}; 

我已經超載>>在客戶和旅遊經營者,這樣的ifstream的可以傳遞給他們,一條線是從物流中取出,標記化和把它放到對象實例變量中。

容器類按順序處理插入操作,數據加載器管理列表並創建ifstream,以便將其傳遞給對象。

這是我的問題:

我加載我的客戶在第一個文件,並填充該列表。

之後,我不得不加載旅遊,其中有顧客的客戶ID預訂他們,我想存儲這些客戶在每個旅遊對象的指針向量,以便客戶信息很容易訪問。

在此刻我存儲customerIDs作爲一個字符串列表,那麼當旅行團都裝,傳遞CUSTLIST成通過CUSTLIST搜索功能,用字符串列表匹配它

這意味着我我不得不維護兩個列表,一個字符串和其他指針,基本上雙處理所有的數據..考慮到數據集是相當大的,這意味着更長的加載時間..

所以我想知道如果有一種方法可以從Tour的overloaded >>運算符中訪問custList實例變量,並在創建Tour對象時生成指針列表?

從技術上講,所有事情都發生在DataLoader類的範圍內,所以我認爲它應該是可能的,但我只是不太確定如何去做..也許使它成爲一個朋友類?我曾嘗試這樣做,但還沒有過迄今爲止任何運氣..

任何幫助,將不勝感激,並遺憾的長篇大論的解釋,希望這是有道理的..

+0

顧客如何與旅遊相關?瞭解這兩種數據類型之間的關係很重要。從邏輯上講,我假設客戶類型擁有旅遊列表。 –

+0

既沒有從彼此繼承,反而是另一種方式,Tour擁有一個客戶列表,這就是爲什麼我需要在創建Tour對象時訪問已經加載的客戶列表。我將更新代碼以反映關係。 – guskenny83

+0

「此刻我將customerIDs存儲爲一個字符串列表,然後當瀏覽器全部加載時,將custList傳遞到一個通過custList進行搜索的函數,並將其與字符串列表進行匹配」我認爲'boost :: serialize '可以在內部處理這種情況。無論如何,當客戶ID列表也應該工作時,爲什麼還需要'Tour'中的指針列表?如果你真的需要指針,你可以通過'>>'的左側提供客戶列表,即'mysource >> someTour',並使用'mysource'的自定義類型。 – dyp

回答

1

最終流的使用可以看起來像這樣的:

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; 
} 
+0

我寧願建議使用客戶列表創建自定義流作爲參數給ctor,而不是使用'operator >>',因爲它不是流提取;但除此之外,+1,我也想在我對OP的評論中提出這種形式。 – dyp

+0

@DyP custStream >>客戶返回>> tours的輸入參數CustomersInFileStream。測試也順利進行,你認爲它會失敗嗎? –

+0

所有運營商都在那裏,但使用錯過的遊覽流,現在修復。 –