Auto-redefining members

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)
I do not know of any way to do this. But it seems like fishy design. Why would this be useful?
You mean something like the following..?

1
2
3
4
5
6
7
8
9
10
11
12
13
struct A{
    static int val;
};

int A::val = 0;

struct B : public A{
};

int main()
{
    B::val = 10;
}


Or did you intend B::val to be a different static variable?
I don't see why this is fishy at all, just not possible at the moment =)

My own needs are like such:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class DLL Filed abstract{
public:
        static Filed* FactoryInstance(char type, ...);
        virtual operator std::string() = 0;

        virtual bool loadFromString(std::string& data, bool edit = false) = 0;
        virtual std::string writeToString() = 0;

        static std::string getFormat(uint form=0);

protected:
        static int varType;
        virtual void setFormat() = 0;
        static std::string format; //use VAR::NEWFORM for additional data layouts
};


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.
Last edited on
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:
    static int val;
};
class B : public A{};

int main(){
   A::val = 5;
   B::val = 10;
   //A::val is still 5
}
Last edited on
@ SatsumaBenji: Except that cire used a struct, so his data members are public, you have to at least make 'val' protected.
Right you are, well I've corrected mine
Any ideas though?
Last edited on
I suspect that I'm dead wrong about what I imagine the OP is trying to do, but I can't help thinking that this is what union() is for. Here's a real life example of what I think they want to do: http://msdn.microsoft.com/en-us/library/windows/desktop/ms221627(v=vs.85).aspx

Can anyone tell me what I'm missing here?
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.
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//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)
Last edited on
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" 
Last edited on
Topic archived. No new replies allowed.