I'm feeling a little confused regarding C++ Composition with overloaded constructors. I have read these other articles here, here, and here but still feel like my question is a little different from those. The first one was the most helpful and I implemented what was suggested there but have a few questions about how to use the composition object.
Here are some code snippits:
// 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();
}
}
}
So my intention is to have two variables of type tempsensor within the habs class and use the overloaded constructor in the temp class at the time of instantiation of those objects. Then a thread such as the one in main.cpp will constantly update the temp sensors while a mutex isn't locked.
I'm hoping someone can explain to me why I have to call the public variables in this fassion: habs_obj.intemp(in).setTemp();
instead of this: habs_obj.extemp.setTemp();
Also am I doing this correctly? Is there another way? I read about the copy constructor and I don't think that is what I need to do but I could be wrong there too.
Thanks in advance!