Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Es ist ja keine Schande etwas falsch zu machen, als Programmierer tu ich das täglich, [...].
Dann sind wir wieder beim Observer-Pattern.
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> unsigned int maxTypeID = 0; template < typename T > unsigned int type() { static unsigned int id = ++maxTypeID; return id; } template < typename T > unsigned int type( T t ) { return type<decltype( t )>(); } void main() { int a = 1; int b = 2; float c = 3; std::cout << type(a) << std::endl; std::cout << type(b) << std::endl; std::cout << type(c) << std::endl; if(type(5) == type<int>()) std::cout << "ok" << std::endl; } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include <iostream> #include <vector> unsigned int maxTypeID = 0; template < typename T > unsigned int type() { static unsigned int id = ++maxTypeID; return id; } template < typename T > unsigned int type( T t ) { return type<decltype( t )>(); } class IEvent { public: IEvent() {} virtual ~IEvent() {} virtual unsigned int Type() = 0; }; template < typename T > class EventType : public IEvent { public: EventType() {} virtual ~EventType() {} unsigned int Type() { return type<T>(); } }; class A : public EventType<A> { public: A() {} ~A() {} }; class B : public EventType<B> { public: B() {} ~B() {} }; void main() { std::vector<IEvent*> events; A e1; B e2; B e3; A e4; A e5; events.push_back( &e1 ); events.push_back( &e2 ); events.push_back( &e3 ); events.push_back( &e4 ); events.push_back( &e5 ); for(IEvent* e : events) std::cout << "Event Type: " << e->Type() << std::endl; } |
Es ist ja keine Schande etwas falsch zu machen, als Programmierer tu ich das täglich, [...].
Zitat von »"Sacaldur"«
Das Problem dabei ist, dass zur Initialisierung diese Funktion erstmal für jeden Typen einmal aufgerufen werden muss.
Mal ganz abgesehen davon, dass die grundsätzliche herangehensweise eher angepasst werden sollte.
Quellcode |
|
1 2 3 4 5 6 7 8 9 |
for (IEvent* e : events) { if (e->Type() == type< A >()) { A* a = static_cast< A* >( e ); a->PrintA(); } else if (e->Type() == type< B >()) { B* b = static_cast< B* >( e ); b->PrintB(); } } |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Xanatus« (29.07.2015, 08:34)
Es ist ja keine Schande etwas falsch zu machen, als Programmierer tu ich das täglich, [...].
Werbeanzeige