It may sound daft to some and genius to others however I doubt this is possible and I want to know if anyone has any ideas?
Basically I want to create a base class which defines a static data member so that its automatically redeclared as the same static data member in the derived class.
1 2 3 4 5 6 7 8 9 10
class A{
protected:
static derivable int val;
// A::val
}
class B : public A{
// static derivable int val is already here
// A::val AND B::val
}
This seems impossible to me but I'm wondering if perhaps there's a way to add modifiers to the compiler to do this (or preferably something MUCH simpler)
It's varType and format that I want to store only once for information about the class it's stored in, storing for specific instances are just wasting space.
The Filed class is used along with a load of functions in the same namespace as itself that handles reading and writing to files all the data in any class that's derived from this class.
The pure virtual functions force the user to define how new classes should use this to read/write there info, and the format of how this info is stored is all held inside string format
I only need to know the format once for each class but I don't want to have to user type static std::string format for every new class.
Pretty much what @cire said.
With no variables defined explicitly in the derived class, the variables are still there from the base class, but it's not the base classes static variable.
1 2 3 4 5 6 7 8 9 10 11
class A{
protected:
staticint val;
};
class B : public A{};
int main(){
A::val = 5;
B::val = 10;
//A::val is still 5
}
I don't think that's what I'm looking for, honestly I don't go too near to unions because I don't see much point in most of my work.
If you're able to think of how this might work in my situation I'd be more than
happy to try but I just can't see it.
A union is shared space between types, and per instance.
I want shared data between instances (static), and per type (redefined in derived class).
It's sorta the complete opposite XD
EDIT: As I said I don't think this is even possible in any way but I thought I'd at least ask because I couldn't find anything through goggling.
No, I don't think it's possible. On the other hand, unless you're developing on 30-year-old hardware, or your format strings are ridiculously enormous, I don't see why you're worrying over trying to save such a tiny amount of space.
Well the format string is storing bytes which indicate what types are stored in a file for loading to a class.
The bytes can indicate primitive or user defined types and I have functions that decide how to read the data depending on the result.
//These are the data help in some classes of mine
vPolygon3D >
int numPoints
pVector3D* points
int numTriangles
Triangle* triangles
format = HOLD_A, FILED, HOLD_A, FILED
//This is simplified as I do have other flags in here
pVector >
double x
double y
double z
format = DOUBLE, DOUBLE, DOUBLE
Triangle >
int A
int B
int C
format = INT, INT, INT
1 2 3 4 5
//Format tells function to read like this...
int arSize
find format[arSize] > (double, double, double)[arSize]
int arSize
find format[arSize] > (int, int, int)[arSize]
This is just a simple example but the principle is that using the format string, no matter how complex an object is, all the user defined classes can be traced and read from/stored to file. (or all data can be saved in form of a string, this string is not the format string)
I was thinking that it could be better just saving the format into a macro but I don't know if there's a way to attach enum data to a "stringified" macro.
Because macros are PRE-processor I imagine that they'll be read before the enumerator type in my coding?
If this does work I'd use it as such wouldn't I?
1 2
#define FORMAT_VPOLY3D #HOLD_A #PVECTOR3D #HOLD_A #TRIANGLE
//Since "a""b""c" should automatically be cut to "abc"