C++ 如何为类的成员实现以下功能?

C++ 如何为类的成员实现以下功能?,c++,C++,下面是一个计时器 template <typename Duration, typename Function> void timer(Duration const & d, Function const & f) { std::thread([d,f](){ std::this_thread::sleep_for(d); f();//error here }).detach(); } 我这样称呼它: timer(st

下面是一个计时器

template <typename Duration, typename Function>
void timer(Duration const & d, Function const & f)
{
    std::thread([d,f](){
        std::this_thread::sleep_for(d);
        f();//error here
    }).detach();
}
我这样称呼它:

timer(std::chrono::milliseconds(10), &myclass::my_functions());

当我尝试在成员函数上调用它时,我得到了错误C2064:term不计算为具有0个参数的函数

这里的问题是非静态成员函数与常规或静态函数不同。它有一个隐藏参数,该参数是指向调用函数的对象的指针

你有几种方法可以解决这个问题。首先,您可以将其设置为静态,然后将其视为一个普通函数,其名称的作用域为类。如果不能这样做,则可以使用创建类似函数的对象,以便调用函数运算符。它的使用方式就像

timer(std::chrono::milliseconds(10), std::bind(&class_name::my_functions(), &class_instance));
最后,您可以使用lambda来包装调用,就像
bind
那样。因此,语法应该是

timer(std::chrono::milliseconds(10), [&class_instance](){
    return class_instance.my_functions(); 
});

这里的问题是,非静态成员函数与常规或静态函数不同。它有一个隐藏参数,该参数是指向调用函数的对象的指针

你有几种方法可以解决这个问题。首先,您可以将其设置为静态,然后将其视为一个普通函数,其名称的作用域为类。如果不能这样做,则可以使用创建类似函数的对象,以便调用函数运算符。它的使用方式就像

timer(std::chrono::milliseconds(10), std::bind(&class_name::my_functions(), &class_instance));
最后,您可以使用lambda来包装调用,就像
bind
那样。因此,语法应该是

timer(std::chrono::milliseconds(10), [&class_instance](){
    return class_instance.my_functions(); 
});

如果没有对象实例,就不能调用非静态方法/函数(即使方法/函数中实际上不需要对象)


要在不需要对象的情况下实现方法调用,请声明该方法
static
(仍然可以在类中,但在其名称之前添加
static

如果没有对象实例,则不能调用非静态方法/函数(即使在方法/函数中并不真正需要该对象)


要在不需要对象的情况下实现方法调用,请声明该方法
static
(仍然可以在类内部,但在其名称之前添加
static

要将非静态成员函数传递给另一个函数,需要使用头中的
std::function
std::bind
,以及实例化对象

myclass mc;
timer(std::chrono::milliseconds(1), std::bind(&myclass::my_functions, mc));
但是,您的代码可能无法按预期工作,因为要查看消息,必须等待线程调用。下面是一个简单的工作示例

#include <thread>
#include <iostream>
#include <functional>
#include <chrono>

template <typename Duration, typename Function>
void timer(Duration const & d, Function const & f)
{
    std::thread([d, f](){
        std::this_thread::sleep_for(d);
        f();//error here
    }).detach();
}

class myclass{
public:
    void my_functions() const {
        std::cout << "aaa";
    }
};

int main(){
    myclass mc;
    timer(std::chrono::milliseconds(1), std::bind(&myclass::my_functions, mc));
    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
}
#包括
#包括
#包括
#包括
模板
无效计时器(持续时间常数和d、功能常数和f)
{
标准::线程([d,f](){
std::this_线程::sleep_for(d);
f();//此处有错误
}).detach();
}
类myclass{
公众:
void my_函数()常量{

std::cout要将一个非静态成员函数传递给另一个函数,需要利用头中的
std::function
std::bind
,并实例化对象

myclass mc;
timer(std::chrono::milliseconds(1), std::bind(&myclass::my_functions, mc));
但是,您的代码可能无法按预期工作,因为要查看消息,您必须等待线程进行调用

#include <thread>
#include <iostream>
#include <functional>
#include <chrono>

template <typename Duration, typename Function>
void timer(Duration const & d, Function const & f)
{
    std::thread([d, f](){
        std::this_thread::sleep_for(d);
        f();//error here
    }).detach();
}

class myclass{
public:
    void my_functions() const {
        std::cout << "aaa";
    }
};

int main(){
    myclass mc;
    timer(std::chrono::milliseconds(1), std::bind(&myclass::my_functions, mc));
    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
}
#包括
#包括
#包括
#包括
模板
无效计时器(持续时间常数和d、功能常数和f)
{
标准::线程([d,f](){
std::this_线程::sleep_for(d);
f();//此处有错误
}).detach();
}
类myclass{
公众:
void my_函数()常量{

std::cout如果您可以对
timer()
函数的参数列表收费,那么这也会起作用

#include <iostream>
#include <chrono>
#include <thread>

template <typename Duration, typename Function, typename Class>
void timer(Duration const & d, Function const & f,Class const& o)
{
    std::thread([d,f,o](){
        std::this_thread::sleep_for(d);
        f(o);//error here
    }).detach();
}
class Foo{
public:
    Foo() {}
    void func() const {
        std::cout<<__func__<<":"<<__LINE__<<std::endl;};
    }

int main(){
    std::function<void(const Foo&)> foo_func = &Foo::func;
    const Foo foo;
    timer(std::chrono::seconds(2),foo_func,foo);
    std::this_thread::sleep_for(std::chrono::seconds(5));
    return 0;
}
#包括
#包括
#包括
模板
无效计时器(持续时间常数和d、函数常数和f、类常数和o)
{
标准::螺纹([d,f,o](){
std::this_线程::sleep_for(d);
f(o);//这里有错误
}).detach();
}
福班{
公众:
Foo(){}
void func()常量{

std::cout如果您可以对
timer()
函数的参数列表收费,那么这也会起作用

#include <iostream>
#include <chrono>
#include <thread>

template <typename Duration, typename Function, typename Class>
void timer(Duration const & d, Function const & f,Class const& o)
{
    std::thread([d,f,o](){
        std::this_thread::sleep_for(d);
        f(o);//error here
    }).detach();
}
class Foo{
public:
    Foo() {}
    void func() const {
        std::cout<<__func__<<":"<<__LINE__<<std::endl;};
    }

int main(){
    std::function<void(const Foo&)> foo_func = &Foo::func;
    const Foo foo;
    timer(std::chrono::seconds(2),foo_func,foo);
    std::this_thread::sleep_for(std::chrono::seconds(5));
    return 0;
}
#包括
#包括
#包括
模板
无效计时器(持续时间常数和d、函数常数和f、类常数和o)
{
标准::螺纹([d,f,o](){
std::this_线程::sleep_for(d);
f(o);//这里有错误
}).detach();
}
福班{
公众:
Foo(){}
void func()常量{

std::coutis它是一个静态成员函数吗?否则,系统如何知道在哪个对象上调用它?这是真正的代码吗?
myclass::my_functions()是什么
return?您是否试图获取指向成员的函数指针?它是静态的吗?@Aganju不,它不是static@MATH000不能像这样引用非静态成员that@buld0zzrmy_函数()显示消息并返回void。它是静态成员函数吗?否则,系统如何知道在哪个对象上调用它?这是真正的代码吗?myclass::my_functions()是什么
return?您是否试图获取指向成员的函数指针?它是静态的吗?@Aganju不,它不是static@MATH000不能像这样引用非静态成员that@buld0zzrmy_函数()显示一条消息并返回void。那么
std::function
std::bind
呢?您的答案不完全取决于OP编写的内容和想要执行的操作,函数实际上是静态的,因此将其设为非静态然后绑定是没有意义的。这是最简单的解决方案,另外两个是技术性工作,这将ld对OO概念的理解很少。因此,我没有列出它们。那么
std::function
std::bind
呢?你的答案并不完整。从OP编写和想要做的事情来看,函数实际上是静态的,所以让它非静态然后绑定它是没有意义的。这是最简单的解决方案,另一个是其中两个是技术性的工作,对OO概念的理解很少。因此,我没有列出它们。