简体   繁体   English

如何从另一个函数调用属于类成员的函数?

[英]How do I call a function that is member of a class, from another function?

So I have the following: 所以我有以下几点:

INT CMainFrm::load(Cstring path)
{

}

extern "C"
{
   EXPORT void fileLoad(CString filePath)
   {
      CMainFrm *cmf = new CMainFrm();
      cmf->load(filePath);
   }
}

Both are located in mainfrm.cpp. 两者都位于mainfrm.cpp中。

fileLoad() is called from a dll and that works fine. 从dll调用fileLoad() ,并且效果很好。 When I try to call load() from inside fileload() it gives me unhandled exception...I also tried calling it using an instance of CMainFrm but the result is the same. 当我尝试从fileload()内部调用load() ,它给了我未处理的异常...我也尝试使用CMainFrm的实例来调用它,但结果是相同的。 Any suggestion on how to do this properly? 关于如何正确执行此操作的任何建议?

You need to call the CMainFrm object that already exists. 您需要调用已经存在的CMainFrm对象。 (It is created at program startup.) MFC provides a global function that you can call to get a pointer to this main window: AfxGetMainWnd(). (它是在程序启动时创建的。)MFC提供了一个全局函数,您可以调用该函数来获得指向该主窗口的指针:AfxGetMainWnd()。 You may cast the returned value to CMainFrm* and use it to call load. 您可以将返回值强制转换为CMainFrm *并使用它来调用load。

You must call Create for dynamically allocated/created MFC classes ( CWnd ), otherwise your m_hWnd will not be valid and your window not created. 您必须为动态分配/创建的MFC类( CWnd )调用Create ,否则,您的m_hWnd将无效且不会创建窗口。
Most likely you will get a exception. 您很可能会得到例外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 How can I call a non-static member function of a class from a static member function of another class? - How can I call a non-static member function of a class from a static member function of another class? 如何通过将类的对象和成员函数传递给C ++中的另一个函数来调用类? - How do I call a class by passing it's object and member function to another function in c++? 我如何获得一个类成员函数来访问另一个类成员函数的私有成员? - How do i get a class member function to have access to another class member function's private member? 如何使用来自另一个 class 的公共成员 function 作为参数调用线程 - How to call thread with a public member function from another class as argument 如何从同一类的另一个成员函数调用函子? - How to call a functor from another member function of the same class? 如何在同一个类中调用另一个运算符重载成员函数的运算符重载成员函数(或使用运算符)? - How can i call a operator overloaded member function(or use the operator) from another operator overloading member function in the same class? 如何调用另一个班级的成员function? - how to call another class's member function? 您如何在类外部的成员函数中调用成员函数? - How do you call a member function in a member function outside of a class? 如何从另一个成员 function 调用成员 function 指针? - How can i call a member function pointer from another member function? 如何从成员类调用函数? - How to call function from a member class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM