Typinformation
-
Hallo,
ich habe folgende Klasse:
class DT_FormatArg { public: DT_FormatArg(); DT_FormatArg( long alValue ); DT_FormatArg( char* apsValue ); DT_FormatArg( DT_String& acValue ); void* GetValue(); bool IsEmpty(); private: void* mpValue; bool mbIsEmpty; };Im Konstruktor kann ich der Klasse long, char* usw. mitgeben.
Jetzt stellt sich für mich die Frage: Was bist du? Ein Long, ein DT_String?
Lässt sich soetwas überhaupt realisieren?Merci
-
warum arbeitest du nicht mit templates?
-
Mal davon abgesehen, dass das etwas krude wirkt, könntest Du sowas wie einen Type-Tag speichern.
Z.B. enum Type { Long, String, WhatEver };
Der wird im Konstruktor entsprechend gesetzt und kann dann wieder abgefragt werden.
Templates sind hier nicht brauchbar, wenn man einen "echten" Variant will.
(es entstehen viele unterschiedliche Typen, man will aber nur einen einzigen)Eine tolle Artikelserie zu Variants kannst Du bei Interesse hier lesen:
http://www.cuj.com/documents/s=7984/cujcexp2004alexandr/alexandr.htm
http://www.cuj.com/documents/s=7982/cujcexp2006alexandr/alexandr.htm
http://www.cuj.com/documents/s=7980/cujcexp2008alexandr/alexandr.htm
-
Tanzfreak schrieb:
ich habe folgende Klasse:
class DT_FormatArg { public: DT_FormatArg(); DT_FormatArg( long alValue ); DT_FormatArg( char* apsValue ); DT_FormatArg( DT_String& acValue ); void* GetValue(); bool IsEmpty(); private: void* mpValue; bool mbIsEmpty; };Im Konstruktor kann ich der Klasse long, char* usw. mitgeben.
Jetzt stellt sich für mich die Frage: Was bist du? Ein Long, ein DT_String?
Lässt sich soetwas überhaupt realisieren?
MerciUnd Polymorphie ist keine Möglichkeit?
class DT_FormatArg { public: FormatArg (bool empty) : empty (empty) {} virtual void* GetValue() = 0; bool IsEmpty() { return empty; } protected: bool empty; }; class DT_FormatArgLong : public FormatArg { public: DT_FormatArgLong (long value) : FormatArg (false) , value (value) {} virtual void* GetValue() { return &value; } private: long value; }; ... // ähnlich für andere TypenODer was ist mit boost::any?