在 C++ 中使用函数指针成员初始化结构数组
Initializing array of structures with function pointer member in C++
我在初始化一个结构数组时遇到问题,其中包含函数指针作为其中的成员。
class Record
{
private:
typedef void (*display_fn_t) ();
struct record {
int a;
display_fn_t disp;
};
static const record rec[];
void disp1() { cout << "Display 1 with a string to display" << endl; }
void disp2() { cout << "Display 2 with an integer to display" << endl; }
public:
int find() { /* logic to find the record comes here */ }
void display() {
for (int i = 0; i < 2; i++) {
rec[i].disp();
}
}
}
const Record::record Record::rec[] = {
{ 10, disp1 },
{ 11, disp2 }
};
int main()
{
Record r;
if (r.find())
r.display();
return 0;
}
当我编译上面的代码时,出现以下编译错误:
mca_record.cpp:56: error: argument of type ‘void (Record::)()’ does
not match ‘void (*)()’
要使调用有效,您必须像这样调用它:
for (int i = 0; i < 2; i++) {
(*rec[i].disp)();
}
并以这种方式初始化 table:
const Record::record Record::rec[] = {
{ 10, &Record::disp1 },
{ 11, &Record::disp2 }
};
您的语法有误,没有使用正确的运算符。
修复大量语法错误,并删除不相关的 find
操作,然后利用适当的成员函数指针和 operator ->*
给出以下内容(执行此操作的几种方法之一):
#include <iostream>
class Record
{
private:
typedef void (Record::*display_memfn_t)();
struct record
{
int a;
display_memfn_t disp;
};
static const record rec[];
void disp1() { std::cout << "Display 1 with a string to display" << std::endl; }
void disp2() { std::cout << "Display 2 with an integer to display" << std::endl; }
public:
void display();
};
const Record::record Record::rec[] =
{
{ 10, &Record::disp1 },
{ 11, &Record::disp2 }
};
void Record::display()
{
for (size_t i=0; i<sizeof rec/sizeof*rec; ++i)
(this->*(rec[i].disp))();
}
int main()
{
Record r;
r.display();
return 0;
}
输出
Display 1 with a string to display
Display 2 with an integer to display
将它与您现有的代码进行比较,特别是指向成员函数的指针不仅仅是指向函数的指针。它们需要不同的处理和通常不同的操作员来使用。 See here 成员访问的不同方法(变量和函数)。
祝你好运。
我在初始化一个结构数组时遇到问题,其中包含函数指针作为其中的成员。
class Record
{
private:
typedef void (*display_fn_t) ();
struct record {
int a;
display_fn_t disp;
};
static const record rec[];
void disp1() { cout << "Display 1 with a string to display" << endl; }
void disp2() { cout << "Display 2 with an integer to display" << endl; }
public:
int find() { /* logic to find the record comes here */ }
void display() {
for (int i = 0; i < 2; i++) {
rec[i].disp();
}
}
}
const Record::record Record::rec[] = {
{ 10, disp1 },
{ 11, disp2 }
};
int main()
{
Record r;
if (r.find())
r.display();
return 0;
}
当我编译上面的代码时,出现以下编译错误:
mca_record.cpp:56: error: argument of type ‘void (Record::)()’ does not match ‘void (*)()’
要使调用有效,您必须像这样调用它:
for (int i = 0; i < 2; i++) {
(*rec[i].disp)();
}
并以这种方式初始化 table:
const Record::record Record::rec[] = {
{ 10, &Record::disp1 },
{ 11, &Record::disp2 }
};
您的语法有误,没有使用正确的运算符。
修复大量语法错误,并删除不相关的 find
操作,然后利用适当的成员函数指针和 operator ->*
给出以下内容(执行此操作的几种方法之一):
#include <iostream>
class Record
{
private:
typedef void (Record::*display_memfn_t)();
struct record
{
int a;
display_memfn_t disp;
};
static const record rec[];
void disp1() { std::cout << "Display 1 with a string to display" << std::endl; }
void disp2() { std::cout << "Display 2 with an integer to display" << std::endl; }
public:
void display();
};
const Record::record Record::rec[] =
{
{ 10, &Record::disp1 },
{ 11, &Record::disp2 }
};
void Record::display()
{
for (size_t i=0; i<sizeof rec/sizeof*rec; ++i)
(this->*(rec[i].disp))();
}
int main()
{
Record r;
r.display();
return 0;
}
输出
Display 1 with a string to display
Display 2 with an integer to display
将它与您现有的代码进行比较,特别是指向成员函数的指针不仅仅是指向函数的指针。它们需要不同的处理和通常不同的操作员来使用。 See here 成员访问的不同方法(变量和函数)。
祝你好运。