Question

Goal:

I have many derived classes (later on there will probably be additional derived classes) which are stored inside a tree structure and I want to serialize each of those classes into a protobuffer and later encrypt the binary data (given by SerializetoArray). But each of those classes should be encrypted with a different key. So obviously if I want to decrypt it I need to know which key was used to encrypt.

Question:

Is there a easy way to assign a unique integer to each of those classes, so that the number is the same across runtimes on every system (Linux, MacOS, Windows etc.) and for every instance of the class? (It should also not change if the class gets a version update.)

One obvious solution would be to define a number manually, but that will be unpractical later on, since I hope that many of those derived classes will be designed by the community and thus it would be almost impossible to guarantee the uniqueness of them.

Is there a better solution?

Was it helpful?

Solution

Here's my two cents:

Add a static const member variable to each class, with the desired value.

That way, each instance of the same class will always have the very same value. something like:

class MyClassA
{
   static const int myVal = 1;
public:
   inline myVal() const { return myVal; }
}

class MyClassB
{
   static const int myVal = 2;
public:
   inline myVal() const { return myVal; }
}

To avoid duplicated values you might create assign them from an enum, for instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top