使用多重继承和模板访问基 类 的成员
Access the members of base classes with multiple inheritance and templates
我希望能够从文件中读取前四个整数,并能够通过它们的名称检索它们:N、H、C、W。
例如:
//some_file.mat: {10,20,30,40.....}
data_format.read_header<N,C,W,H>(some_file.mat);
cout << data_format.getN(); // prints the first read integer: 10
cout << data_format.getH(); // prints the fourth read integer: 40
data_format.read_header<C,N,H,W>(some_file.mat);
cout << data_format.getN(); // prints the second read integer: 20
cout << data_format.getH(); // prints the fourth read integer: 30
以下代码尝试使用多重继承和模板来实现此目的:
struct N{ int val; };
struct C{ int val; };
struct H{ int val; };
struct W{ int val; };
struct DataFormat : N, C, H, W
{
template<class T1, class T2, class T3, class T4>
bool read_header(FILE* p_file)
{
int res = 0;
res += fread(&T1::val, sizeof(int), 1, p_file); //doesn't compile
res += fread(&T2::val, sizeof(int), 1, p_file); //doesn't compile
res += fread(&T3::val, sizeof(int), 1, p_file); //doesn't compile
res += fread(&T4::val, sizeof(int), 1, p_file); //doesn't compile
return (res != 0);
}
int getN(){ return N::val; }
int getC(){ return C::val; }
int getH(){ return H::val; }
int getW(){ return W::val; }
};
static void foo(){
DataFormat data_format;
FILE* some_file;
data_format.read_header<N, W, H, C>(some_file);
}
我收到下一条编译器错误消息:在以 res+=fread...
开头的所有行上
error C2664: 'size_t fread(void *,size_t,size_t,FILE )' : cannot
convert argument 1 from 'int N:: ' to 'void *'
为什么?
对更优雅的解决方案有什么建议吗?
&T1::val
将 return 一个 成员指针 (即类似 N::*
的东西),而不是指向变量的指针(即 int*
) 如您所料。
您可以添加 this
限定符来访问基 class 中的成员变量,例如:
res += fread(&this->T1::val, sizeof(int), 1, p_file);
res += fread(&this->T2::val, sizeof(int), 1, p_file);
res += fread(&this->T3::val, sizeof(int), 1, p_file);
res += fread(&this->T4::val, sizeof(int), 1, p_file);
我希望能够从文件中读取前四个整数,并能够通过它们的名称检索它们:N、H、C、W。 例如:
//some_file.mat: {10,20,30,40.....}
data_format.read_header<N,C,W,H>(some_file.mat);
cout << data_format.getN(); // prints the first read integer: 10
cout << data_format.getH(); // prints the fourth read integer: 40
data_format.read_header<C,N,H,W>(some_file.mat);
cout << data_format.getN(); // prints the second read integer: 20
cout << data_format.getH(); // prints the fourth read integer: 30
以下代码尝试使用多重继承和模板来实现此目的:
struct N{ int val; };
struct C{ int val; };
struct H{ int val; };
struct W{ int val; };
struct DataFormat : N, C, H, W
{
template<class T1, class T2, class T3, class T4>
bool read_header(FILE* p_file)
{
int res = 0;
res += fread(&T1::val, sizeof(int), 1, p_file); //doesn't compile
res += fread(&T2::val, sizeof(int), 1, p_file); //doesn't compile
res += fread(&T3::val, sizeof(int), 1, p_file); //doesn't compile
res += fread(&T4::val, sizeof(int), 1, p_file); //doesn't compile
return (res != 0);
}
int getN(){ return N::val; }
int getC(){ return C::val; }
int getH(){ return H::val; }
int getW(){ return W::val; }
};
static void foo(){
DataFormat data_format;
FILE* some_file;
data_format.read_header<N, W, H, C>(some_file);
}
我收到下一条编译器错误消息:在以 res+=fread...
error C2664: 'size_t fread(void *,size_t,size_t,FILE )' : cannot convert argument 1 from 'int N:: ' to 'void *'
为什么? 对更优雅的解决方案有什么建议吗?
&T1::val
将 return 一个 成员指针 (即类似 N::*
的东西),而不是指向变量的指针(即 int*
) 如您所料。
您可以添加 this
限定符来访问基 class 中的成员变量,例如:
res += fread(&this->T1::val, sizeof(int), 1, p_file);
res += fread(&this->T2::val, sizeof(int), 1, p_file);
res += fread(&this->T3::val, sizeof(int), 1, p_file);
res += fread(&this->T4::val, sizeof(int), 1, p_file);