0

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!

Michael Riley
  • 67
  • 1
  • 10
  • 2
    Totally unrelated to your problem or your question, but why are you using a `typedef` for the `adconf` structure? Structures are just like classes in C++, the structure name is a type-name just like for classes. – Some programmer dude Jul 23 '17 at 15:29
  • 1
    A good place to start are [these C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). If you see a tutorial that has `using namespace std;` in it, you might want to find another resource. – Ron Jul 23 '17 at 15:31
  • Wow, two function declarations in a single line! Pretty unreadable if you ask me :) – Rakete1111 Jul 23 '17 at 15:31
  • I am using typedef vs class because the data attributes contained within are constant. There really is not justifiable reason for using typedef vs class but it is easy to test quickly. There are not two function definitions in a single line. The usage of namespace std is a debatable topic that I will not entertain here. There are different schools of thought on the subject. Additionally if your comment is not constructive type elsewhere. – Michael Riley Jul 23 '17 at 17:13

0 Answers0