以下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.
答案 0 :(得分:2)
Functors旨在充当普通的旧C函数,这意味着它们应该很小,或者至少很容易复制。函数通常按值传递。以标准库<algorithm>
为例。
但是即使你使用operator()
来做一些不仅仅是一个简单函数的东西,即你的函子里面有很多状态和机器,你也不应该担心复制ctors,析构函数等。启用优化后,大部分对象绕过将被删除。
如果您不信任编译器技能,最好的解决方案是使用std::ref()
和std::cref()
,它们使用正确的值语义创建引用。