gpt4 book ai didi

c++ - 如何通过引用或指针传递功能对象(仿函数)?

转载 作者:行者123 更新时间:2023-11-28 02:37:37 31 4
gpt4 key购买 nike

以下 C++ 代码是对我要解决的问题的简化。

它生成一个随机数,然后调用功能对象做出决定并生成输出。

代码详细说明了一个功能对象、一个函数和主程序。提供了示例输出。我添加了复制构造函数、复制赋值、移动构造函数和移动赋值代码来尝试阐明这里发生了什么。

我对使用函数对象还比较陌生,一直在努力弄清楚为什么对复制构造函数、移动构造函数和析构函数的调用如此之多。事实上,对析构函数的过多调用在我的实际应用程序中造成了麻烦,其中功能对象在其构造函数中分配了资源。

我的问题是:

有没有办法修改此代码,以便传递指向功能对象的指针或引用?这样就不需要复制、移动或销毁对象,除非程序完成?

#include <functional>
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <thread>

class RelativeValue
{
public:
//Constructor
RelativeValue(const int bdry) : boundary(bdry)
{
std::cout << "RelativeValue: Constructor" << std::endl;
}

//Copy Constructor
RelativeValue(const RelativeValue &orig) : boundary(orig.boundary)
{
std::cout << "RelativeValue: Copy Constructor" << std::endl;
}

//Copy assignment
RelativeValue& operator= (const RelativeValue &that)
{
std::cout << "RelativeValue: Copy Assignment Constructor" << std::endl;
if (this != &that)
{
boundary = that.boundary;
}
return *this;
}

//Move constructor
RelativeValue(RelativeValue &&orig) /*noexcept NOT VS2013*/ : boundary(orig.boundary)
{
std::cout << "RelativeValue: Move Constructor" << std::endl;
}

//Move Assignment
RelativeValue& operator=(RelativeValue &&that) /*noexcept NOT VS2013*/
{
std::cout << "RelativeValue: Move Assignment Constructor" << std::endl;
if (this != &that)
{
boundary = that.boundary;
}
return *this;
}

//Operators
bool operator==(const RelativeValue &anotherRelativeValue) const
{
return (boundary == anotherRelativeValue.boundary);
}

void operator()(const int &in)
{
if (in < boundary)
{
std::cout << in << " < " << boundary << std::endl;
}
else if (in > boundary)
{
std::cout << in << " > " << boundary << std::endl;
}
else if (in == boundary)
{
std::cout << in << " == " << boundary << std::endl;
}
}

~RelativeValue()
{
std::cout << "RelativeValue: Destructor Called." << std::endl;
}

private:
int boundary;
};


void reactor(const int &iterations, const std::function <void(int)> &functionObject)
{
int runLoop(0);
int number(0);
int returnValue(0);
srand(time(NULL));
while (runLoop < iterations)
{
number = rand() % 100 + 1;//in the range 1 to 100
functionObject(number);//call the functional-object
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
++runLoop;
}
}


int main(int argc, char* argv[])
{
//Want to create the functional-object here.
RelativeValue rv(50);
//Then pass the functional object to the reactor.
std::thread t1(reactor, 10, rv);//do the work.
t1.join();//wait for it.....
return 0;
}

输出:

RelativeValue: Constructor
RelativeValue: Copy Constructor
RelativeValue: Move Constructor
RelativeValue: Move Constructor
RelativeValue: Move Constructor
RelativeValue: Copy Constructor
RelativeValue: Destructor Called.
85 > 50
RelativeValue: Destructor Called.
RelativeValue: Destructor Called.
63 > 50
47 < 50
92 > 50
80 > 50
12 < 50
57 > 50
10 < 50
66 > 50
89 > 50
RelativeValue: Destructor Called.
RelativeValue: Destructor Called.
RelativeValue: Destructor Called.

最佳答案

Functors 被设计为作为增强的普通旧 C 函数,这意味着它们应该很小,或者至少应该易于复制。函数通常按值传递。拿标准库<algorithm>以s为例。

但即使你使用operator()对于不仅仅是简单函数的东西,即你的仿函数内部携带大量状态和机制,你不应该担心复制构造函数、析构函数等。在发布构建期间,优化启用了大部分对象绕过 周围将被删除。

如果您不信任编译器技能,最好的解决方案是使用 std::ref()std::cref() ,它创建具有正确值语义的引用。

关于c++ - 如何通过引用或指针传递功能对象(仿函数)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26979220/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com