简体   繁体   English

成员 function 指的是对象的成员 function

[英]member function referring to an object's member function

I'm a c++ noob so apologies in advance if this question is not properly structured.我是 c++ 菜鸟,所以如果这个问题的结构不正确,请提前道歉。

I have a class which instantiates an object of a different class, I'd like for the member function of the main class (excuse my naive language) to be in turn referring to the member function of the object instantiated in the main class. I have a class which instantiates an object of a different class, I'd like for the member function of the main class (excuse my naive language) to be in turn referring to the member function of the object instantiated in the main class.

I've attached a snippet for reference我附上了一个片段供参考

foo.h foo.h

class foo {
public : 

int data;
void printFunc(){
cout<< "data is"<< data;
}

};

bar.h酒吧.h

class bar {

public:
void updatebarr ();
}

bar.cpp酒吧.cpp

bar::bar()
{
foo foo1;
foo1.data = 1; 
// the line below is flagged as syntax error
// I want the object of type bar's updatebarr() function to be essentially be calling 
// foo::PrintFunc(). I figured this "should" work as the signatures are identical.
this->updatebarr = &foo1.printFunc(); 
}

The error I get is "Reference to non-static member function must be called",我得到的错误是“必须调用对非静态成员 function 的引用”,

The signatures might look similar but these are not free functions but member functions .签名可能看起来很相似,但它们不是自由函数而是成员函数 Have a look at this faq!看看这个常见问题解答!

The type of the first one is: void (foo::*)() and the second is void (bar::*)() .第一个的类型是: void (foo::*)() ,第二个是void (bar::*)() See the difference?看到不同?

They can only be invoked on an object of their respective type.它们只能在各自类型的 object 上调用。 What you can do is this:你可以做的是:

class bar {
  public:
    void updatebarr(foo& f) {
      f.printFunc();
    }
};

Pointers to member methods do not work the way you are trying.指向成员方法的指针不像您尝试的那样工作。 Not only what you have is a syntax error, but it is also a logic error since foo1 will go out of scope and be destroyed when bar() exits, thus leaving updatebarr to call printFunc() on an invalid foo object.您所拥有的不仅是语法错误,而且还是逻辑错误,因为foo1将 go 超出 scope 并在bar()退出时被销毁,从而让updatebarr在无效的foo ZA8CFDE6331AC4BEB26 上调用printFunc()

For what you are attempting, you would need something more like this:对于您正在尝试的内容,您需要更多类似的东西:

foo.h foo.h

class foo {
public : 
    int data;
    void printFunc(){
        cout << "data is " << data;
    }
};

bar.h酒吧.h

#include "foo.h"

class bar {
private:
    foo foo1;
public:
    bar();
    void updatebarr();
};

bar.cpp酒吧.cpp

#include "bar.h"

bar::bar()
{
    foo1.data = 1; 
}

void bar::updatebarr()
{
    foo1.printFunc();
}

main.cpp主文件

#include "bar.h"

int main()
{
    bar b;
    b.updatebarr();
    return 0;
}

Live Demo现场演示

Alternatively, you can make updatebarr() be a std::function that calls printFunc() via a lambda, eg:或者,您可以使updatebarr()成为std::function通过 lambda 调用printFunc() ,例如:

foo.h foo.h

class foo {
public : 
    int data;
    void printFunc(){
      cout << "data is " << data;
    }
};

bar.h酒吧.h

#include <functional>
#include "foo.h"

class bar {
private:
    foo foo1;
public:
    bar();
    std::function<void()> updatebarr;
};

bar.cpp酒吧.cpp

#include "bar.h"

bar::bar()
{
    foo1.data = 1; 
    updatebarr = [&](){ foo1.printFunc(); };
}

main.cpp主文件

#include "bar.h"

int main()
{
    bar b;
    b.updatebarr();
    return 0;
}

Live Demo现场演示

In which case, you could then have the lambda capture a copy of the foo object instead, which will give you a result very close to your original attempt, eg:在这种情况下,您可以让 lambda 捕获foo object的副本,这将为您提供非常接近原始尝试的结果,例如:

foo.h foo.h

class foo {
public : 
    int data;
    void printFunc() const {
      cout << "data is " << data;
    }
};

bar.h酒吧.h

#include <functional>

class bar {
public:
    bar();
    std::function<void()> updatebarr;
};

bar.cpp酒吧.cpp

#include "bar.h"
#include "foo.h"

bar::bar()
{
    foo foo1;
    foo1.data = 1; 
    updatebarr = [foo1](){ foo1.printFunc(); };
}

main.cpp主文件

#include "bar.h"

int main()
{
    bar b;
    b.updatebarr();
    return 0;
}

Live Demo现场演示

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

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