如何显示成员函数指针的值? [英] How to display the value of member function pointers?

查看:63
本文介绍了如何显示成员函数指针的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




抱歉天真的问题。我在这里做的是检查

指向成员函数的指针的值,并尝试通过

成员函数指针调用这些成员函数。但是用gcc编译的程序发出了

段错误。我的代码出了什么问题?


这是代码:

#include< iostream>


using namespace std;


class A {

public:

int method1(int a,int b){cout< ;<"您好,世界英寸;返回0; }

int method2(int a,int b){return a + b; }

int method3(int x,int y){return x * x;}

};

void conv_ptr(void * ptr, char * buf)

{

char * str =(char *)ptr;

int i;

for(i = 0; i< 4; i ++)

sprintf(buf + i * 2,"%。2x",str [i]);

buf [8] = 0;

}

int main()

{

A a;


int(A :: * ptr)(int,int);

char buf [9];


ptr =& A :: method1;

conv_ptr(& ptr,buf);

cout<<" ptr ="<<< buf< < endl;


cout<<(a。* ptr)(5,5)<< endl;

ptr =& A :: method2;

conv_ptr(& ptr,buf);

cout<<"" ptr ="<<< buf<< endl;


cout<<(a。* ptr)(5,5)<< endl;


ptr =& A: :method3;

conv_ptr(& ptr,buf);

cout<<"" ptr ="<<< buf<< endl;


cout<<(a。 * ptr)(5,5)<< endl;

}

谢谢!


Andy

解决方案

Andy写道:

抱歉天真的问题。我在这里做的是检查
指向成员函数的指针的值,并尝试通过
成员函数指针调用这些成员函数。但用gcc编译的程序给出了段故障。


哪里?

我的代码出了什么问题?


有几件事。

这是代码:

#include< iostream>

using namespace std;

A类{
public:
int method1(int a,int b){cout<<" Hello,world" ;;返回0; } int /> int method2(int a,int b){return a + b; } int /> int method3(int x,int y){return x * x;}
};

void conv_ptr(void * ptr,char * buf)
{
char * str =(char *)ptr;
int i;
for(i = 0; i< 4; i ++)
sprintf(buf + i * 2 ,%。2x,str [i]);


我不相信你不会在这里超过缓冲区。

点%。2x中的点是什么?难道你不能只写%02x并且

确保你通过(str [i]& 255)?你看,''x''假设你在int中传递了

,而你实际传入了一个char ...

buf [8] = 0;
}

int main()
{
A a;

int(A :: * ptr)(int,int);
char buf [9];

ptr =& A :: method1;
conv_ptr(& ptr,buf);


BTW,你为什么要传递''ptr''的*地址*而不是''ptr''

本身?

cout<<"" ptr ="<<< buf<< endl;

cout<<(a。* ptr)(5,5)< < endl;

ptr =& A :: method2;
conv_ptr(& ptr,buf);
cout<<"" ptr ="< < buf<< endl;

cout<<(a。* ptr)(5,5)<< endl;

ptr =& A :: method3;
conv_ptr(& ptr,buf);
cout<<"" ptr ="<<< buf<< endl;

cout< ;<(a。* ptr)(5,5)<< endl;
}



V


Andy写道:



抱歉天真的问题。我在这里做的是检查
指向成员函数的指针的值,并尝试通过
成员函数指针调用这些成员函数。但用gcc编译的程序给出了段故障。我的代码有什么问题?

这是代码:

#include< iostream>

使用命名空间std;

A类{
公共:
int method1(int a,int b){cout<<" Hello,world" ;;返回0; } int /> int method2(int a,int b){return a + b; } int /> int method3(int x,int y){return x * x;}
};

void conv_ptr(void * ptr,char * buf)
{
char * str =(char *)ptr;
int i;
for(i = 0; i< 4; i ++)
sprintf(buf + i * 2 ,%。2x,str [i]);
buf [8] = 0;
}

int main()
{
A a;

int(A :: * ptr)(int,int);
char buf [9];

ptr =& A: :method1;
conv_ptr(& ptr,buf);
cout<<"" ptr ="<<< buf<< endl;

cout< <(a。* ptr)(5,5)<< endl;

ptr =& A :: method2;
conv_ptr(& ptr,buf); < br> cout<<" ptr ="<<< buf<< endl;

cout<<(a。* ptr)(5,5)<< endl;

ptr =& A :: method3;
conv_ptr(& ptr,buf);
cout<<"" ptr ="<< buf<< endl;

cout<<(a。* ptr)(5,5)<< endl;
}

谢谢!

安迪


cout<< &安培; PTR;输出指针的地址。你的conv_ptr()函数

是没有必要的。


当你指向一个成员函数时,你不能输入ptr =

& A :: member;你输入ptr = a.member;


请看以下内容:


A级{

public:

void method1(){cout<< A :: method1(),名为\ n; }

};

int main(){

A a;

void(A :: * ptr )(无效);

ptr = a.method1;

cout<< & ptr<< endl;

(a。* ptr)();

返回0;

}


Ken Human写道:

[...]
当你指向一个成员函数时,你不能输入ptr =
& A ::会员;你输入ptr = a.member;


嗯?

请看以下内容:

A级{
公开:
void method1 (){cout<< A :: method1(),名为\ n; }
};

int main(){
A a;
void(A :: * ptr)(void);

ptr = a.method1;
cout<< & ptr<< endl;
(a。* ptr)();
返回0;
}




它不编译。我会留给你解决。


V


Hi,

Sorry for the naive question. What I did here is to check the value of
pointers to member functions, and try to call these member function via
member functio pointers. But the program compiled with gcc gives out
segment fault. What''s wrong with my code?

Here is the code:
#include <iostream>

using namespace std;

class A{
public:
int method1(int a, int b){ cout<<"Hello,world"; return 0; }
int method2(int a, int b) { return a + b; }
int method3(int x, int y) { return x * x;}
};
void conv_ptr(void * ptr, char* buf)
{
char * str = (char*)ptr;
int i;
for(i = 0; i < 4 ; i++)
sprintf(buf+i*2, "%.2x", str[i]);
buf[8] = 0;
}
int main()
{
A a;

int (A::*ptr) (int, int) ;
char buf[9];

ptr = &A::method1;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;
ptr = &A::method2;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;

ptr = &A::method3;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;
}
Thanks !

Andy

解决方案

Andy wrote:

Sorry for the naive question. What I did here is to check the value of
pointers to member functions, and try to call these member function via
member functio pointers. But the program compiled with gcc gives out
segment fault.
Where?
What''s wrong with my code?
Several things.

Here is the code:
#include <iostream>

using namespace std;

class A{
public:
int method1(int a, int b){ cout<<"Hello,world"; return 0; }
int method2(int a, int b) { return a + b; }
int method3(int x, int y) { return x * x;}
};
void conv_ptr(void * ptr, char* buf)
{
char * str = (char*)ptr;
int i;
for(i = 0; i < 4 ; i++)
sprintf(buf+i*2, "%.2x", str[i]);
I am not convinced that you''re not overrunning the buffer here.
What''s the dot for in "%.2x"? Couldn''t you just write "%02x" and
make sure you pass (str[i] & 255)? You see, ''x'' assumes you passed
in an int, while you actually pass in a char...
buf[8] = 0;
}
int main()
{
A a;

int (A::*ptr) (int, int) ;
char buf[9];

ptr = &A::method1;
conv_ptr(&ptr, buf);
BTW, why are you passing in the *address* of ''ptr'' and not the ''ptr''
itself?
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;
ptr = &A::method2;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;

ptr = &A::method3;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;
}


V


Andy wrote:

Hi,

Sorry for the naive question. What I did here is to check the value of
pointers to member functions, and try to call these member function via
member functio pointers. But the program compiled with gcc gives out
segment fault. What''s wrong with my code?

Here is the code:
#include <iostream>

using namespace std;

class A{
public:
int method1(int a, int b){ cout<<"Hello,world"; return 0; }
int method2(int a, int b) { return a + b; }
int method3(int x, int y) { return x * x;}
};
void conv_ptr(void * ptr, char* buf)
{
char * str = (char*)ptr;
int i;
for(i = 0; i < 4 ; i++)
sprintf(buf+i*2, "%.2x", str[i]);
buf[8] = 0;
}
int main()
{
A a;

int (A::*ptr) (int, int) ;
char buf[9];

ptr = &A::method1;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;
ptr = &A::method2;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;

ptr = &A::method3;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;
}
Thanks !

Andy



cout << &ptr; outputs the address of pointer. Your conv_ptr() function
isn''t necessary.

When you point towards a member function, you don''t type ptr =
&A::member; you type ptr = a.member;

Look at the following:

class A {
public:
void method1() { cout << "A::method1() called\n"; }
};
int main() {
A a;
void (A::* ptr)(void);
ptr = a.method1;
cout << &ptr << endl;
(a.*ptr)();
return 0;
}


Ken Human wrote:

[...]
When you point towards a member function, you don''t type ptr =
&A::member; you type ptr = a.member;
Huh?

Look at the following:

class A {
public:
void method1() { cout << "A::method1() called\n"; }
};
int main() {
A a;
void (A::* ptr)(void);
ptr = a.method1;
cout << &ptr << endl;
(a.*ptr)();
return 0;
}



It does not compile. I''ll leave it to you to fix.

V


这篇关于如何显示成员函数指针的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆