c++里是否有方法指针? 如果有该如何使用? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你到底是什么意思。在C++里面都不讲方法的,只讲函数。方法是java的 方法是什么?知道C++有函数指针void CRunDllDlg::OnOK() { HINSTANCE hinstLib; //typedef VOID (*MYPROC)(LPTSTR); typedef VOID (*MYPROC)(CString); MYPROC ProcAdd; CString str_Path = "" , str_Temp = ""; char ch_Path[1024] = { 0 }; CFileFind m_File; int n_Times = 0; GetModuleFileName( NULL , ch_Path , 256 ); str_Path = ch_Path; str_Temp = str_Path.Left( str_Path.GetLength() - 10 ); str_Path = str_Temp; str_Path = str_Path + "Xagent.dll"; BOOL bWorking = m_File.FindFile( str_Path ); while( bWorking ){ bWorking = m_File.FindNextFile(); if ( m_File.IsDots() ) continue; if ( m_File.IsDirectory() ) continue; n_Times ++; } if( n_Times != 1 ){ MessageBox( "对不起!您没有购买该模块。" ); return; } //从这里开始调用Dll,以上部分是查看Dll是否存在 hinstLib = LoadLibrary("Xagent.dll"); if (hinstLib == NULL){ MessageBox( "调用Xagent.dll出错。" ); FreeLibrary(hinstLib); return; }else{ ProcAdd = (MYPROC) GetProcAddress(hinstLib, "SetServerIP"); if( ProcAdd == NULL ){ MessageBox( "调用函数出错SetServerIP。" ); FreeLibrary(hinstLib); return; }else{ ProcAdd( "192.168.0.123" ); } } FreeLibrary(hinstLib); //CDialog::OnOK();} typedef int(*FUNC) (int a,int b); //声明FUNC是一个函数指针类型FUNC func; //func为一个有两个整形参数,返回整数的函数指针。 class CMySocket {public: void get();};CMySocket::get(){ }如何得到get()函数的指针? 大概是这样的吧:具体记不得了CMySocket::*p = CMySocket::get()==== 谢谢robothn(雷鸟)的回答,可是不对... #include "stdafx.h"#include <iostream>using namespace std;class A{public: void Display() { cout << "Hello world!" << endl; }};int main(int argc, char* argv[]){ void (A::*p)() = A::Display; A a; (a.*p)(); return 0;}这样使用 类的非静态成员函数都有隐含的this指针,当然不能直接用指针去访问。否则你岂不是可以从外面访问类的私有部分了!你要用指针,必须用类的静态函数。 xkak2(乾坤一掷)兄说的也对。我也想不通。请liu_feng_fly(笑看风云 搏击苍穹 衔日月) 出来讲讲原因啊 A a;(a.*p)();我是这样调用的,通过一个类的实例a调用,当然就有this指针了。也可以通过指针,比如A* pA = new A;(pA->*p)();这样就可以通过一个类的指针,在搭配一个指向类的成员函数的指针进行使用了。总之,必须先有一个类的实例,然后才能使用指向类的成员函数的指针。 对于一个智能指针(smart point),应该重载operator ->*,原因也就在这里 狄仁杰--宦海浮沉,心系民生 求助,webbrowser控件的弹窗问题 急求用子对话框向父对话框上ListCtrl上指定的列传递参数?? List Control的困惑 取出数据库中某个表的一个列的平均数,取出来数是错误的。 新手首次发问:有谁研究过多重继承下如何实现序列化的功能? 关于agent,100分! 做了个对话框,在主程序中用这个对话框类声明了一个变量,然后用非模式窗口创建显示,如何正确销毁 极其简单的汇编问题,高手请进 WM_NOTIFY,classWizard中没有,如何加入? 怎样截获动态生成的控件的消息?<急> 从二进制文件中读数据的问题?
在C++里面都不讲方法的,只讲函数。
方法是java的
知道C++有函数指针
void CRunDllDlg::OnOK()
{
HINSTANCE hinstLib;
//typedef VOID (*MYPROC)(LPTSTR);
typedef VOID (*MYPROC)(CString);
MYPROC ProcAdd; CString str_Path = "" , str_Temp = "";
char ch_Path[1024] = { 0 };
CFileFind m_File;
int n_Times = 0; GetModuleFileName( NULL , ch_Path , 256 );
str_Path = ch_Path; str_Temp = str_Path.Left( str_Path.GetLength() - 10 );
str_Path = str_Temp;
str_Path = str_Path + "Xagent.dll"; BOOL bWorking = m_File.FindFile( str_Path );
while( bWorking ){
bWorking = m_File.FindNextFile();
if ( m_File.IsDots() )
continue;
if ( m_File.IsDirectory() )
continue;
n_Times ++;
}
if( n_Times != 1 ){
MessageBox( "对不起!您没有购买该模块。" );
return;
} //从这里开始调用Dll,以上部分是查看Dll是否存在
hinstLib = LoadLibrary("Xagent.dll"); if (hinstLib == NULL){
MessageBox( "调用Xagent.dll出错。" );
FreeLibrary(hinstLib);
return;
}else{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "SetServerIP");
if( ProcAdd == NULL ){
MessageBox( "调用函数出错SetServerIP。" );
FreeLibrary(hinstLib);
return;
}else{
ProcAdd( "192.168.0.123" );
}
}
FreeLibrary(hinstLib);
//CDialog::OnOK();
}
FUNC func; //func为一个有两个整形参数,返回整数的函数指针。
{
public:
void get();
};
CMySocket::get()
{
}如何得到get()函数的指针?
CMySocket::*p = CMySocket::get()====
#include <iostream>
using namespace std;class A
{
public:
void Display()
{
cout << "Hello world!" << endl;
}
};int main(int argc, char* argv[])
{
void (A::*p)() = A::Display;
A a;
(a.*p)();
return 0;
}
这样使用
你要用指针,必须用类的静态函数。
(a.*p)();
我是这样调用的,通过一个类的实例a调用,当然就有this指针了。也可以通过指针,比如
A* pA = new A;
(pA->*p)();
这样就可以通过一个类的指针,在搭配一个指向类的成员函数的指针进行使用了。总之,必须先有一个类的实例,然后才能使用指向类的成员函数的指针。