C ++如何将类的类型成员作为模板参数传递?

时间:2020-09-17 21:26:00

标签: c++ file object types

我正在尝试创建文件管理器,但是在构造函数中存在一个问题。

这是记录结构。

struct Records {
    char code[5];
    char name[20];
    char career[15];

    Records() = default;

    Records(string code, string name, string career) {
        // ...
        // Constructor
        // ...
    }
};

这是文件管理器类,具有两个模板参数。例如,我不会使用 MembType 来按代码,姓名或职业对记录的载体进行排序;然后将每条记录插入文件。

template <typename ObjType, typename MembType>
class SequentialFile {
    string name_file;
    string aux_file;
    fstream file;
    MembType primary_key;

public:
    template <MembType>
    SequentialFile(string name_file, MembType primary_key) {
        this->name_file = name_file + ".dat";
        this->aux_file = name_file + "_aux.dat";
        this->primary_key = primary_key;
        ofstream (this->name_file);
        ofstream (this->aux_file);
    }
    
    void insertAll(vector<ObjType> records) {
        file.open(this->name_file, ios::in | ios::out | ios::binary);

        if(file.is_open()) {
            //-I use primary_key here
            sort(records.begin(), records.end(), [this](const ObjType& r1, const ObjType& r2) {
                return strcmp(r1.*primary_key, r2.*primary_key) < 0;
            });

            file.seekg(0, ios::end);

            for(const auto& record : records) {
                file.write((char*)& record, sizeof(ObjType));
            }

            file.close();
        } else {
            cerr << "Can't open file " << this->name_file << endl;
        }
    }
};

我的问题是,我必须在 XXX 中插入什么?

int main(int argc, char const *argv[]) {
    Records record1("0001", "User1", "Career1");
    Records record2("0002", "User2", "Career2");
    Records record3("0003", "User2", "Career3");

    vector<Register> records;
    records.push_back(record1);
    records.push_back(record2);
    records.push_back(record3);

    SequentialFile<Record, XXX> test1("test1", &Record::name);

    return 0;
}

1 个答案:

答案 0 :(得分:0)

@JerryJeremiah的代码

#include <iostream>
using std::cout;
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <string.h>
#include <algorithm>
using std::sort;

struct Records {
    char code[5];
    char name[20];
    char career[15];

    Records() = default;

    Records(string code, string name, string career) {
        strcpy(this->code, code.c_str());
        strcpy(this->name, name.c_str());
        strcpy(this->career, career.c_str());
    }
};

template <typename ObjType, typename MembType>
class SequentialFile {
    string name_file;
    MembType primary_key;

public:
    SequentialFile(string name_file, MembType primary_key) {
        this->name_file = name_file + ".dat";
        this->primary_key = primary_key;
    }

    void insertAll(vector<ObjType> records) {
        //-Add sort to test
        sort(records.begin(), records.end(), [this](const ObjType& r1, const ObjType& r2) {
            return strcmp(r1.*primary_key, r2.*primary_key) < 0;
        });


        for(Records record: records)
            cout << record.name << "\n";
    }
};

int main(int argc, char const *argv[]) {
    Records record1("0001", "User1", "Career1");
    Records record2("0002", "User2", "Career2");
    Records record3("0003", "User2", "Career3");

    vector<Records> records;
    records.push_back(record1);
    records.push_back(record2);
    records.push_back(record3);
    
    // the field we want to use is "char name[20]" from Records so we pass "char (Records::*)[20]"
    SequentialFile<Records,char (Records::*)[20]> test1("test1", &Records::name);

    test1.insertAll(records);

    return 0;
}

相关问题