![](/img/trans.png)
[英]Passing C++ class member function as a paremeter to C function
[英]C++ How to pass a type member of a class as a template paremeter?
我正在嘗試創建一個文件管理器,但我在構造函數中遇到了一個問題。
這是記錄結構。
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;
}
@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.