繁体   English   中英

从另一类调用一个类的成员函数

[英]Calling member function of one class from another

我在一段包含一些事件处理对象的代码中遇到了这个问题。 在事件处理类中,我试图通过指针调用另一个类的函数,但是它与该代码所具有的错误相同,我已经实现了该错误,只是检查了我要用来在那里调用的逻辑。 我在这里做什么错?

#include <iostream>
using namespace std;
class FunctionPointer;
class UseFP
{
public:
    UseFP(void){}
    ~UseFP(void){}

    void Modified(int m,int n);
    void (FunctionPointer::*update)(int,int);

};

void UseFP::Modified(int m,int n)
    {
            //(this->*update)(m,n);// call by fp if I uncomment it it gives error.
    }

class FunctionPointer
{
    int a,b;
    UseFP * obj;
public:
    FunctionPointer(void);

    ~FunctionPointer(void);

    void updateData(int m, int n)
    {
        a = m;
        b = n;
        cout<<"\n\nUpdated: a "<<a<<", b "<<b<<endl;
    }

    void Input()
    {
        int m, n;
        cout<<"\nEnter new data: ";
        cin>>m>>n;
        obj->Modified(m,n);
    }
    };

void main()
{
    FunctionPointer obj;
    obj.Input();
}

取消注释函数调用后出现错误

1>------ Build started: Project: FunctionPointer, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\users\volmo\desktop\functionpointer\functionpointer\functionpointer.h(14) : error C2440: 'newline' : cannot convert from 'UseFP *const ' to 'FunctionPointer *const '
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\volmo\desktop\functionpointer\functionpointer\functionpointer.h(14) : error C2647: '->*' : cannot dereference a 'void (__thiscall FunctionPointer::* )(int,int)' on a 'UseFP *const '
1>Generating Code...
1>Compiling...
1>FunctionPointer.cpp
1>c:\users\volmo\desktop\functionpointer\functionpointer\functionpointer.h(14) : error C2440: 'newline' : cannot convert from 'UseFP *const ' to 'FunctionPointer *const '
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\volmo\desktop\functionpointer\functionpointer\functionpointer.h(14) : error C2647: '->*' : cannot dereference a 'void (__thiscall FunctionPointer::* )(int,int)' on a 'UseFP *const '
1>Generating Code...
1>Build log was saved at "file://c:\Users\volmo\Desktop\FunctionPointer\FunctionPointer\Debug\BuildLog.htm"
1>FunctionPointer - 4 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

update的类型是“指向类的成员函数FunctionPointer ,这意味着它需要一个FunctionPointer上的左实例->* 。但是你要取消对它的引用与this是类型UseFP因此错误。

您需要一个FunctionPointer实例来调用它的update 我不知道您想要的语义是什么,但是一种获取语义的方法是向Modified添加参数:

void UseFP::Modified(FunctionPointer &fp, int m,int n)
    {
            (fp.*update)(m,n);
    }

你不能做这样的事情。 您应该将FunctionPointer对象传递给Modified函数,或存储为类变量。

void UseFP::Modified(FunctionPointer* p, int m,int n)
    {
       (p->*update)(m,n);// call by fp if I uncomment it it gives error.
    }

并称它为

obj->Modified(this,m,n);

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM