我对带有重载构造函数的 C++ 组合感到有些困惑。我已经在这里、这里和这里阅读了这些其他文章,但仍然觉得我的问题与那些有点不同。第一个是最有帮助的,我实现了那里的建议,但对如何使用组合对象有一些疑问。
以下是一些代码片段:
// From temp.cpp
namespace tmp
{
typedef struct
{
unsigned long adcmemory;
uint32_t adcinputstuff;
}adconf;
class tempsensor
{
private:
double temp;
unsigned char memloc;
public:
tempsensor();
tempsensor(adconf setup)
{...}
// More stuff below
.
.
};
// From habs.hpp
#include <temp.cpp>
const tmp::adconf ex = {ADC_MEM0, ADC_INPUT_A22};
const tmp::adconf in = {ADC_MEM1, ADC_INPUT_A23};
class habs
{
public:
tmp::tempsensor extemp(tmp::adconf ex), intemp(tmp::adconf ex);
// More stuff below
};
// In main.cpp
void tempThread(habs::habs &habs_obj)
{ /*
This thread will update the internal and external temp sensor values
while the mutex is not locked. When the mutex is locked the thread
will spin wait.
*/
while(1)
{
while(!habs_obj.getMutex())
{
// This is what I expected to see but I get an error
// #302 a pointer to a bound function may only be used to call
// the function habs_obj.extemp.setTemp();
habs_obj.extemp.setTemp();
habs_obj.extemp(ex).setTemp();
habs_obj.intemp(in).setTemp();
}
}
}
所以我的意图是在 habs 类中有两个 tempsensor 类型的变量,并在实例化这些对象时使用 temp 类中的重载构造函数。然后,诸如 main.cpp 中的线程之类的线程将在互斥锁未锁定的情况下不断更新温度传感器。
我希望有人能向我解释为什么我必须在这个 fassion 中调用公共变量: habs_obj.intemp(in).setTemp();
而不是这个: habs_obj.extemp.setTemp();
我也这样做正确吗?还有其他方法吗?我阅读了有关复制构造函数的信息,但我认为这不是我需要做的,但我也可能错了。
提前致谢!