Hello,
I could use some help :) I have two (actually many many more) different objects that I use to transform data; AES and DES (encryption). I only know what transformation type I need at run time. All transformation objects (AES, DES, etc) have the same transformation call but different implementations.
Here is a very simple idea of what I am trying to do (The following does not work)
class AES {
public:
AES();
~AES();
void TransformData() {
// AES specific implementation stuff
printf("AES TRANSFORM DONE\n");
}
private:
};
class DES
{
public:
DES();
~DES();
void TransformData() {
// DES specific implementation stuff
printf("DES TRANSFORM DONE\n");
}
private:
};
template <class T> class Both:public AES, public DES
{
public:
void TransformData() {
// This obviously doesn't work
T->TransformData(); // ????
};
private:
};
int main(int argc, char* argv[])
{
// A generic pointer
Both<> *myTransform;
// Define the pointer as an AES object
myTransform = new Both<AES>;
// Call the AES transform
myTransform.TransformData();
// Free the AES object
delete(myTransform);
// Now define the pointer as a DES object
myTransform = new Both<DES>;
// Call DES transform
myTransform.TransformData();
// clean up
delete(myTransform);
return 0;
}
The above desired output would be:
AES TRANSFORM DONE
DES TRANSFORM DONE
Now I know the above does not work but the general idea would allow me to simply declare the object type at run time, call transform and have the object's method perform the correct transformation.
I currently have it working by declaring pointers to objects for each transformation type and then in a large if statement, creating the object for each type as needed and calling the transform function... But if I could do something like the above, the code would be much cleaner.
(Hope I explained it well)
Thanks
Ard
class Transformer
{
// other stuff, such as the data unless it will be a
// parameter to transformData().
public:
virtualvoid transformData() = 0;
};
class AES: public Transformer
{
public:
virtualvoid transformData() {
// AES-specific implementation
}
};
class DES: public Transformer
{
public:
virtualvoid transformData() {
// DES-specific implementation
}
};
// usage:
AES * obj1 = new AES();
DES * obj2 = new DES();
obj1->transformData();
obj2->transformData();
// transformData() can be called from a base class pointer, too
Transformer * obj( obj1 );
obj->transformData();
delete obj1;
delete obj2;
I'm confused, because you say that you only know the transformation at runtime, but your example is of a case where you know the transformation at compile time.
Nonetheless, if I can guess what you really want, it sounds like the virtual
mechanism is what you need. Something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class Data {};
class Encrypter {
public:
virtualvoid TransformData( Data& ) const = 0;
};
class AES : public Encrypter {
public:
virtualvoid TransformData( Data& ) const;
};
class DES : public Encrypter {
public:
virtualvoid TransformData( Data& ) const;
};
Hello,
Thank you for your replies.
jsmith: You are correct, I did not give and example of run time but compile time; the example was to confer the idea that I need to be able to dynamically use the same pointer for what ever type as needed...
I finally came up with the following; it is not as clean as I hope and I am sure there is a better way to do this...
#define AES_TYPE 1
#define DES_TYPE 2
class AES {
public:
void TransformData() {
// AES specific implementation stuff
printf("AES TRANSFORM DONE\n");
}
private:
};
class DES
{
public:
void TransformData() {
// DES specific implementation stuff
printf("DES TRANSFORM DONE\n");
}
private:
};
class Both:public AES, public DES
{
public:
Both(int in_type) {my_type = in_type;};
void TransformData() {
if (my_type == DES_TYPE) DES::TransformData();
elseif (my_type == AES_TYPE) AES::TransformData();
};
private:
int my_type;
};
int main(int argc, char* argv[])
{
// A generic pointer for my transform
Both *myTransform;
// Define the pointer as an AES object
myTransform = new Both(AES_TYPE);
// Call AES transform
myTransform->TransformData();
// clean up
delete(myTransform);
// Now define the pointer as a DES object
myTransform = new Both(DES_TYPE);
// Call DES transform
myTransform->TransformData();
// clean up
delete(myTransform);
return 0;
}
The idea is to have a single object type that I can interface with and not the multiple different encryption types; hence why Both inherits both AES and DES.
Thank you for your help :)