Muperman
Muperman

Reputation: 384

working with templates and multiple constructors

I just started using templates and c++.

I want to add a templated class as private inside another class.

I want to add this Class:

Header:

template <class type>
class BufferedDatenKonsistenz {
public:
    BufferedDatenKonsistenz(type* buffer0, type* buffer1, type* buffer2);
    BufferedDatenKonsistenz(type* buffer0, type* buffer1);

    //some methods
private:
    const int16_t numberOfBuffers;

    type* buffers[3];

    //some other members
};

Source:

template<class type>
BufferedDatenKonsistenz<type>::BufferedDatenKonsistenz(type* buffer0, type* buffer1, type* buffer2) : numberOfBuffers(3){
    this->buffers[0] = buffer0;
    this->buffers[1] = buffer1;
    this->buffers[2] = buffer2;
}

template<class type>
BufferedDatenKonsistenz<type>::BufferedDatenKonsistenz(type* buffer0, type* buffer1) : numberOfBuffers(2){
    this->buffers[0] = buffer0;
    this->buffers[1] = buffer1;
    this->buffers[2] = NULL;
}

And add this class as private member in some other class:

Other Class header:

class SomeClass {
public:
    SomeClass();
    ~SomeClass();

    //some stuff

private:

    //some stuff

    static const uint16_t cyclicDataSize = 50;
    uint16_t cyclicDataArea0[cyclicDataSize];
    uint16_t cyclicDataArea1[cyclicDataSize];
    uint16_t cyclicDataArea2[cyclicDataSize];

    // How do I get this right???????????????????????????????????????????????????
    DatenKonsistenz::BufferedDatenKonsistenz<uint16_t> bufferLogik(cyclicDataArea0,
                                                                   cyclicDataArea1,
                                                                   cyclicDataArea2);
};

I don't know if the thing I am trying is even possible. But if it's possible I don't know if the template-stuff is wrong or if I maybe have to move everything inside the constructors into the initializer list or whatever.

There are too many potential errors here I can't figure it out myself atm.

Btw. I am stuck to C++03, because the compiler can't do anything newer.

Upvotes: 1

Views: 163

Answers (1)

Max Langhof
Max Langhof

Reputation: 23691

You should be able to safely initialize them in the constructor of SomeClass, for example in the initialization list:

SomeClass::SomeClass()
  : bufferLogik(cyclicDataArea0, cyclicDataArea1, cyclicDataArea2)
{}

Upvotes: 2

Related Questions