Static members & Inheritance

Hi again all;

I have a base class, which contains some structures, that I want included in some derived classes as a single instance, so that whenever a derived class looks at the structure it is effectively a global structure.

The easy way would be making it global and using pointers to pass the structure to the class, but is there a way of doing it with inheritance?

The idea is shown below, but this does not compile. It gets the idea across:

class base{
protected:
struct dataStructure{
int var1;
int var2;
};
static dataStructure myData;
}

class derivedOne: protected base{
public:
getVar1();
getVar2();
setVar1();
setVar2();
}

class derivedTwo: protected base{
public:
getVar1();
getVar2();
setVar1();
setVar2();
}

The idea is shown below, but this does not compile. It gets the idea across:

No, it doesn't. We have no idea why it doesn't compile. We could guess, but it's better if you tell us what the compiler makes of that incomplete program.

PaulS:

The idea is shown below, but this does not compile. It gets the idea across:

No, it doesn't. We have no idea why it doesn't compile. We could guess, but it's better if you tell us what the compiler makes of that incomplete program.

SERIAL.cpp.o: In function `Input':
C:\.../DERIVED.cpp:10: undefined reference to `Base::myData'
C:\.../DERIVED.cpp:11: undefined reference to `Base::myData'
C:\.../DERIVED.cpp:12: undefined reference to `Base::myData'
DERIVED.cpp.o: In function `Derived::getVar1()':
C:\.../DERIVED.cpp:45: undefined reference to `Base::myData'
DERIVED.cpp.o: In function `Derived::getVar2()':
C:\.../DERIVED.cpp:72: undefined reference to `Base::myData'
DERIVED.cpp.o:C:\.../DERIVED.cpp:77: more undefined references to `Base::myData' follow

How about showing the whole code, huh? What you posted doesn't give those error messages. It gives:

sketch_dec07a:11: error: ISO C++ forbids declaration of 'getVar1' with no type
sketch_dec07a:12: error: ISO C++ forbids declaration of 'getVar2' with no type
sketch_dec07a:13: error: ISO C++ forbids declaration of 'setVar1' with no type
sketch_dec07a:14: error: ISO C++ forbids declaration of 'setVar2' with no type
sketch_dec07a:19: error: ISO C++ forbids declaration of 'getVar1' with no type
sketch_dec07a:20: error: ISO C++ forbids declaration of 'getVar2' with no type
sketch_dec07a:21: error: ISO C++ forbids declaration of 'setVar1' with no type
sketch_dec07a:22: error: ISO C++ forbids declaration of 'setVar2' with no type
sketch_dec07a:23: error: expected unqualified-id at end of input

Tweaking it a bit to get rid of those errors leads to a clean compile, which doesn't therefore address your question of static members and inheritance:

class base{
protected:
struct dataStructure{
int var1;
int var2;
};
static dataStructure myData;
};

class derivedOne: protected base{
public:
void getVar1();
void getVar2();
void setVar1();
void setVar2();
};

class derivedTwo: protected base{
public:
void getVar1();
void getVar2();
void setVar1();
void setVar2();
};

void setup () {}
void loop () {}

It's no good posting hypothetical code to "get the idea across".

The easy way would be making it global and using pointers to pass the structure to the class, but is there a way of doing it with inheritance?

I don't see how that would work. Pointers to what? Which instance of the class?

I can’t post the full code I’m afraid, not my decision.

Pointers to a global declaration of the structure, which gets passed into other classes, is straight forward and would work however I want to try and do this with inheritance so that the structure is within it’s own class (with other structures). NOT pointers.

The problem being, each derived class would have it’s own instance of the structures and therefore the structure data is not shared between ALL classes.

My idea is that if the structures are STATIC, when each derived class gets constructed they all look at the same memory, therefore the data is shared by definition.

Forget the code I posted, it’s confusing matters, apologies.

So in summary I want to be able to:

Have a class of structures and members, that are inherited by other classes. The whole system must only have one instance of the structures which is shared by many different classes.

The whole system must only have one instance of the structures which is shared by many different classes.

One instance of the structure instance that is shared by all instances of the base and derived classes? Or one instance of the definition of the structure that is shared by all instances of base and derived classes? It isn't clear which you mean.

The first one.

The structure will contain data about the whole system (similar to a registry), which may be read or written to by any derived class.

However, there is only one registry of numbers for the entire system.

PROBLEM SOLVED

For any onlookers!

The key is to remember to put "myClass::myClass myClass::myStructure;" in the base class cpp.

The static definition in the header sets up the memory and the definition in the cpp actually puts something in memory for your derived classess to access. Previously there was a variable there, but just empty memory, no structure to access.

Thanks to Owen McAree for this!

In the base class header:

class myClass{
public:
  struct dataStruct{
    short* myShortP;
    char*  myCharP;
  };
  static dataStruct  myStructure;

In the base class cpp:

#include "MYCLASS.h"

myClass::myClass  myClass::myStructure;

In the derived class header:

class myDerivedClass: 
protected myClass{
  void myFunction;
  short* myPointer;
}

In the derived class cpp:

#include "MYDERIVEDCLASS.h"

void myDerivedClass::myFunction(){
  myStructure.myShortP = myPointer;
}

howroyd:
I can’t post the full code I’m afraid, not my decision.

Maybe you aren’t in a position to publish your complete application for some reason, but if there is any significant complexity in it then we don’t want you to anyway. Produce the simplest sketch / program which demonstrates your problem, and post it in its entirety. (Quite often, when you strip away all the extraneous code you’ll find that the answer to your problem is staring you in the face anyway.)