lundi 2 mars 2020

Best practice: Initialization of static private parameters problem when inerits in different classes

I have two classes Class A and Class B objects, that inherits from Class C. Class C containts static private parameters that I would like to initialize with different values in Class A and Class B. Class A and Class B are in fact containers filled with some mathematical operations. And Class B takes a Class A element as argument in the constructor.

As I initialize the static private parameters of Class C at the begining of Class A and Class B, I get a multiple definition ofC::m_privateParam'` error.

Please find below a scheme of the setup and some information about how the classes are implemented: enter image description here

A.H

class A: public C
{
public:
    Type methodsA()
private:
    Type m_paramA;
}

A.CPP

#include "B.h"
#include "C.h"

Type C::m_paramC = something;

Type A::methodsA()
{
}

It is similar for Class B without the include to B and the static parameter from C should be with different value compared to A Type C::m_paramC = something_else;.

C.H

class C
{
public:
Template<class T>
Type C::methodsC()
{
   /* use of m_paramC */
}
private:
    static Type m_paramC;
}

The template<T> in C.H allows to perform the same operations on objects from A or B. So, my question is the following: what is the best practice to link together classes on the way described above and be able to change in A and B the values of the private variables of C?

Aucun commentaire:

Enregistrer un commentaire