class test
{
void (*p)();
public:
void Print(){cout<<"Test"<<endl;}
test(){p = Print;}
};
我本来想定义指向成员函数的指针*p,可惜不能指向Print函数给出错误
error C2440: '=' : cannot convert from 'void (__thiscall test::*)(void)' to 'void (__cdecl *)(void)'
There is no context in which this conversion is possible
17 个解决方案
#1
这样定义试一下
class test
{
void (*p)(test *);
...
}
class test
{
void (*p)(test *);
...
}
#2
class test
{
void (test::*p)();
public:
void Print(){cout<<"Test"<<endl;}
test(){p = &test::Print;}
};
{
void (test::*p)();
public:
void Print(){cout<<"Test"<<endl;}
test(){p = &test::Print;}
};
#3
都不行
#4
谢谢 hz129(古雨) 搞定
不过可以不用p=&test::Print;可以直接就用p=Print;
不过可以不用p=&test::Print;可以直接就用p=Print;
#5
you should declare a point to a member function outside the class, otherwise you would use the point in a ugly express like this:
#include <iostream>
using namespace std;
class test
{
public:
void (test::*p)();
void Print(){cout<<"Test"<<endl;}
test(){p = &test::Print;}
};
void (test::*q)();
main() {
test t;
q = &test::Print;
(t.*q)();
(t.*(t.p))(); //the ugly express
}
#include <iostream>
using namespace std;
class test
{
public:
void (test::*p)();
void Print(){cout<<"Test"<<endl;}
test(){p = &test::Print;}
};
void (test::*q)();
main() {
test t;
q = &test::Print;
(t.*q)();
(t.*(t.p))(); //the ugly express
}
#6
I use g++, it must use "p=&test::Print;", and this is more clearly.
#7
But how can I use in the inner of the class?
#8
what's your mean?
#9
I means that How can I used the 'p' in the class funtion?
#10
Oh, I can use it like this:
#include <iostream.h>
class test
{
int idx;
public:
void (test::*p)();
void Print(){cout<<"Test : "<<idx<<endl;}
test(int _idx)
{
idx = _idx;
p = Print;
(this->*(test::p))(); // NOTE:
//NOTE: if I change it to "(*(test::p))();" , the complier gave me a Error:
// error C2171: '*' : illegal on operands of type 'void (__thiscall test::*)(void)'
// if I change it to "((test::p))();" ,it still gave me :
//error C2064: term does not evaluate to a function
// I don't know why
}
};
int main(int argc, char* argv[])
{
test t1(100),t2(200);
void (test::*q)();
q = &test::Print;
(t1.*q)();
q = t2.p;
(t2.*q)();
(t1.*(t1.p))(); //the ugly express
return 0;
}
#include <iostream.h>
class test
{
int idx;
public:
void (test::*p)();
void Print(){cout<<"Test : "<<idx<<endl;}
test(int _idx)
{
idx = _idx;
p = Print;
(this->*(test::p))(); // NOTE:
//NOTE: if I change it to "(*(test::p))();" , the complier gave me a Error:
// error C2171: '*' : illegal on operands of type 'void (__thiscall test::*)(void)'
// if I change it to "((test::p))();" ,it still gave me :
//error C2064: term does not evaluate to a function
// I don't know why
}
};
int main(int argc, char* argv[])
{
test t1(100),t2(200);
void (test::*q)();
q = &test::Print;
(t1.*q)();
q = t2.p;
(t2.*q)();
(t1.*(t1.p))(); //the ugly express
return 0;
}
#11
NOTE行也可改为
(this->*p)();
(this->*p)();
#12
(this->*(test::p))(); // NOTE:
可写成
(this->*p)(); // NOTE:
用指向成员函数的指针进行调用时,必须指明它的调用者。
"((test::p))();"的错误在于test::p只是限定了调用本类的p函数,但事实上p是指向函数的指针,其本身不是函数,所以出错。
另:
q = t2.p;
这一行可不用,q申明为指向test类的Print函数,对于test类来说,不论生成多少个对象,其Print函数不会因为调用对象的不同而导致行为不同,所以test类只需有一份Print函数的拷贝就行了。这时用q进行调用,只要限定调用它的对象就行了,而不必再次进行赋值,除非你想让它指向另一个不同的函数。
可写成
(this->*p)(); // NOTE:
用指向成员函数的指针进行调用时,必须指明它的调用者。
"((test::p))();"的错误在于test::p只是限定了调用本类的p函数,但事实上p是指向函数的指针,其本身不是函数,所以出错。
另:
q = t2.p;
这一行可不用,q申明为指向test类的Print函数,对于test类来说,不论生成多少个对象,其Print函数不会因为调用对象的不同而导致行为不同,所以test类只需有一份Print函数的拷贝就行了。这时用q进行调用,只要限定调用它的对象就行了,而不必再次进行赋值,除非你想让它指向另一个不同的函数。
#13
#include <iostream.h>
class test
{
int idx;
public:
void (test::*p)();
void (test::*arrp[10])();
void Print(){cout<<"Test::Print "<<idx<<endl;}
test(int _idx)
{
cout<<endl<<"__________Begin Construct__________"<<endl;
idx = _idx;
cout<<"The value is : "<<idx<<endl;
p = Print;
cout<<"Call (this->*p)() : ======> ";
(this->*p)(); // NOTE: if I change it to "(*(test::p))();"
arrp[0] = p;
cout<<"Call (this->*arrp[0])() =====> ";
(this->*arrp[0])();
cout<<"~~~~~~~~~~End Construct~~~~~~~~~~~~"<<endl;
}
};
int main(int argc, char* argv[])
{
cout<<"Initialize"<<endl;
test t1(100),t2(200);
void (test::*q)();
q = &test::Print;
// 一般用法:
cout<<endl<<"(t1.*q)() =======> "; (t1.*q)(); //NOTE
cout<<endl<<"(t2.*q)() =======> "; (t2.*q)();
// NOTE: q并非是test的成员变量或成员函数, 而*q为test的一个成员函数
// 注意运行结果
cout<<endl<<"(t1.*(t1.p))(); =======> "; (t1.*(t1.p))();
cout<<endl<<"(t1.*(t2.p))(); =======> "; (t1.*(t2.p))();
cout<<endl<<"(t2.*(t1.p))(); =======> "; (t2.*(t1.p))();
cout<<endl<<"(t2.*(t2.p))(); =======> "; (t2.*(t2.p))();
return 0;
}
上面有关NOTE的一行猪是对吗?
class test
{
int idx;
public:
void (test::*p)();
void (test::*arrp[10])();
void Print(){cout<<"Test::Print "<<idx<<endl;}
test(int _idx)
{
cout<<endl<<"__________Begin Construct__________"<<endl;
idx = _idx;
cout<<"The value is : "<<idx<<endl;
p = Print;
cout<<"Call (this->*p)() : ======> ";
(this->*p)(); // NOTE: if I change it to "(*(test::p))();"
arrp[0] = p;
cout<<"Call (this->*arrp[0])() =====> ";
(this->*arrp[0])();
cout<<"~~~~~~~~~~End Construct~~~~~~~~~~~~"<<endl;
}
};
int main(int argc, char* argv[])
{
cout<<"Initialize"<<endl;
test t1(100),t2(200);
void (test::*q)();
q = &test::Print;
// 一般用法:
cout<<endl<<"(t1.*q)() =======> "; (t1.*q)(); //NOTE
cout<<endl<<"(t2.*q)() =======> "; (t2.*q)();
// NOTE: q并非是test的成员变量或成员函数, 而*q为test的一个成员函数
// 注意运行结果
cout<<endl<<"(t1.*(t1.p))(); =======> "; (t1.*(t1.p))();
cout<<endl<<"(t1.*(t2.p))(); =======> "; (t1.*(t2.p))();
cout<<endl<<"(t2.*(t1.p))(); =======> "; (t2.*(t1.p))();
cout<<endl<<"(t2.*(t2.p))(); =======> "; (t2.*(t2.p))();
return 0;
}
上面有关NOTE的一行猪是对吗?
#14
Sorry,猪是——注释
#15
对
#16
consider it as a static function?
#17
可以,没问题
#1
这样定义试一下
class test
{
void (*p)(test *);
...
}
class test
{
void (*p)(test *);
...
}
#2
class test
{
void (test::*p)();
public:
void Print(){cout<<"Test"<<endl;}
test(){p = &test::Print;}
};
{
void (test::*p)();
public:
void Print(){cout<<"Test"<<endl;}
test(){p = &test::Print;}
};
#3
都不行
#4
谢谢 hz129(古雨) 搞定
不过可以不用p=&test::Print;可以直接就用p=Print;
不过可以不用p=&test::Print;可以直接就用p=Print;
#5
you should declare a point to a member function outside the class, otherwise you would use the point in a ugly express like this:
#include <iostream>
using namespace std;
class test
{
public:
void (test::*p)();
void Print(){cout<<"Test"<<endl;}
test(){p = &test::Print;}
};
void (test::*q)();
main() {
test t;
q = &test::Print;
(t.*q)();
(t.*(t.p))(); //the ugly express
}
#include <iostream>
using namespace std;
class test
{
public:
void (test::*p)();
void Print(){cout<<"Test"<<endl;}
test(){p = &test::Print;}
};
void (test::*q)();
main() {
test t;
q = &test::Print;
(t.*q)();
(t.*(t.p))(); //the ugly express
}
#6
I use g++, it must use "p=&test::Print;", and this is more clearly.
#7
But how can I use in the inner of the class?
#8
what's your mean?
#9
I means that How can I used the 'p' in the class funtion?
#10
Oh, I can use it like this:
#include <iostream.h>
class test
{
int idx;
public:
void (test::*p)();
void Print(){cout<<"Test : "<<idx<<endl;}
test(int _idx)
{
idx = _idx;
p = Print;
(this->*(test::p))(); // NOTE:
//NOTE: if I change it to "(*(test::p))();" , the complier gave me a Error:
// error C2171: '*' : illegal on operands of type 'void (__thiscall test::*)(void)'
// if I change it to "((test::p))();" ,it still gave me :
//error C2064: term does not evaluate to a function
// I don't know why
}
};
int main(int argc, char* argv[])
{
test t1(100),t2(200);
void (test::*q)();
q = &test::Print;
(t1.*q)();
q = t2.p;
(t2.*q)();
(t1.*(t1.p))(); //the ugly express
return 0;
}
#include <iostream.h>
class test
{
int idx;
public:
void (test::*p)();
void Print(){cout<<"Test : "<<idx<<endl;}
test(int _idx)
{
idx = _idx;
p = Print;
(this->*(test::p))(); // NOTE:
//NOTE: if I change it to "(*(test::p))();" , the complier gave me a Error:
// error C2171: '*' : illegal on operands of type 'void (__thiscall test::*)(void)'
// if I change it to "((test::p))();" ,it still gave me :
//error C2064: term does not evaluate to a function
// I don't know why
}
};
int main(int argc, char* argv[])
{
test t1(100),t2(200);
void (test::*q)();
q = &test::Print;
(t1.*q)();
q = t2.p;
(t2.*q)();
(t1.*(t1.p))(); //the ugly express
return 0;
}
#11
NOTE行也可改为
(this->*p)();
(this->*p)();
#12
(this->*(test::p))(); // NOTE:
可写成
(this->*p)(); // NOTE:
用指向成员函数的指针进行调用时,必须指明它的调用者。
"((test::p))();"的错误在于test::p只是限定了调用本类的p函数,但事实上p是指向函数的指针,其本身不是函数,所以出错。
另:
q = t2.p;
这一行可不用,q申明为指向test类的Print函数,对于test类来说,不论生成多少个对象,其Print函数不会因为调用对象的不同而导致行为不同,所以test类只需有一份Print函数的拷贝就行了。这时用q进行调用,只要限定调用它的对象就行了,而不必再次进行赋值,除非你想让它指向另一个不同的函数。
可写成
(this->*p)(); // NOTE:
用指向成员函数的指针进行调用时,必须指明它的调用者。
"((test::p))();"的错误在于test::p只是限定了调用本类的p函数,但事实上p是指向函数的指针,其本身不是函数,所以出错。
另:
q = t2.p;
这一行可不用,q申明为指向test类的Print函数,对于test类来说,不论生成多少个对象,其Print函数不会因为调用对象的不同而导致行为不同,所以test类只需有一份Print函数的拷贝就行了。这时用q进行调用,只要限定调用它的对象就行了,而不必再次进行赋值,除非你想让它指向另一个不同的函数。
#13
#include <iostream.h>
class test
{
int idx;
public:
void (test::*p)();
void (test::*arrp[10])();
void Print(){cout<<"Test::Print "<<idx<<endl;}
test(int _idx)
{
cout<<endl<<"__________Begin Construct__________"<<endl;
idx = _idx;
cout<<"The value is : "<<idx<<endl;
p = Print;
cout<<"Call (this->*p)() : ======> ";
(this->*p)(); // NOTE: if I change it to "(*(test::p))();"
arrp[0] = p;
cout<<"Call (this->*arrp[0])() =====> ";
(this->*arrp[0])();
cout<<"~~~~~~~~~~End Construct~~~~~~~~~~~~"<<endl;
}
};
int main(int argc, char* argv[])
{
cout<<"Initialize"<<endl;
test t1(100),t2(200);
void (test::*q)();
q = &test::Print;
// 一般用法:
cout<<endl<<"(t1.*q)() =======> "; (t1.*q)(); //NOTE
cout<<endl<<"(t2.*q)() =======> "; (t2.*q)();
// NOTE: q并非是test的成员变量或成员函数, 而*q为test的一个成员函数
// 注意运行结果
cout<<endl<<"(t1.*(t1.p))(); =======> "; (t1.*(t1.p))();
cout<<endl<<"(t1.*(t2.p))(); =======> "; (t1.*(t2.p))();
cout<<endl<<"(t2.*(t1.p))(); =======> "; (t2.*(t1.p))();
cout<<endl<<"(t2.*(t2.p))(); =======> "; (t2.*(t2.p))();
return 0;
}
上面有关NOTE的一行猪是对吗?
class test
{
int idx;
public:
void (test::*p)();
void (test::*arrp[10])();
void Print(){cout<<"Test::Print "<<idx<<endl;}
test(int _idx)
{
cout<<endl<<"__________Begin Construct__________"<<endl;
idx = _idx;
cout<<"The value is : "<<idx<<endl;
p = Print;
cout<<"Call (this->*p)() : ======> ";
(this->*p)(); // NOTE: if I change it to "(*(test::p))();"
arrp[0] = p;
cout<<"Call (this->*arrp[0])() =====> ";
(this->*arrp[0])();
cout<<"~~~~~~~~~~End Construct~~~~~~~~~~~~"<<endl;
}
};
int main(int argc, char* argv[])
{
cout<<"Initialize"<<endl;
test t1(100),t2(200);
void (test::*q)();
q = &test::Print;
// 一般用法:
cout<<endl<<"(t1.*q)() =======> "; (t1.*q)(); //NOTE
cout<<endl<<"(t2.*q)() =======> "; (t2.*q)();
// NOTE: q并非是test的成员变量或成员函数, 而*q为test的一个成员函数
// 注意运行结果
cout<<endl<<"(t1.*(t1.p))(); =======> "; (t1.*(t1.p))();
cout<<endl<<"(t1.*(t2.p))(); =======> "; (t1.*(t2.p))();
cout<<endl<<"(t2.*(t1.p))(); =======> "; (t2.*(t1.p))();
cout<<endl<<"(t2.*(t2.p))(); =======> "; (t2.*(t2.p))();
return 0;
}
上面有关NOTE的一行猪是对吗?
#14
Sorry,猪是——注释
#15
对
#16
consider it as a static function?
#17
可以,没问题