简体   繁体   English

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

[英]C++ How to pass a type member of a class as a template paremeter?

I'm trying to create a file manager, but I have one problem in the constructor.我正在尝试创建一个文件管理器,但我在构造函数中遇到了一个问题。

This is the record struct.这是记录结构。

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

    Records() = default;

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

This is the File Manager class that have two template parameters.这是具有两个模板参数的文件管理器类。 I wan't to use MembType , for example, to sort the vector of records by code, name or career;例如,我不想使用MembType来按代码、姓名或职业对记录向量进行排序; then insert each record to the file.然后将每条记录插入到文件中。

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

My question is, what I have to insert in XXX ?.我的问题是,我必须在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;
}

Code by @JerryJeremiah @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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Passing C++ class member function as a paremeter to C function - Passing C++ class member function as a paremeter to C function 如何在构造函数中传递 C++ 模板 class 成员的初始值设定项? - How to pass initializers for the C++ template class member in the constructor? operator &lt;&lt; C ++中模板类中成员类型的重载 - operator<< overload for a member type in a template class in C++ C ++模板类的成员函数中的类型/值不匹配 - Type/value mismatch in member function of C++ template class C ++在构造函数中选择类成员的模板类型 - C++ choose template type of class member in constructor 初始化依赖于私有模板类型(C ++)的静态类成员 - Initializing a static class member that depends on a private template type (C++) 如何将c ++类的静态成员函数传递给模板并调用该函数 - How to pass Static member function of a c++ Class to template and invoke the function c++ class 嵌套模板类型的成员变量 - c++ class member variable of the nested template type 如何在 c++ 的 class 的成员 function 中传递带有模板变量的结构作为参数? - How to pass structure with template variable as an argument inside member function of class in c++? 如何在 C++ 模板 class arguments 中传递成员数组的初始化值? - How to pass initialization values for the member array in C++ template class arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM