PHP-Code:
#include <iostream> using namespace std ; class Test { private: int Val ; public: Test () : Val ( 0 ){ cout << "test ctor" << endl ;} ~ Test (){ cout << "test dtor" << endl ;} int GetVal ( void ){return Val ;} void SetVal (const int & val ){ Val = val ;} }; //bla bla class template <class T > class MC { private: T * obj ; //Pointer to Object MC < T >* List; //Listpointer (start) MC < T >* right ; //next MC < T >* left ; //last MC < T >* end ; //Listpointer (end) int count ; //count for Index public: MC < T >( void ) :List( 0 ), right ( 0 ), left ( 0 ), end ( 0 ), count ( 0 ) { cout << "MC ctor" << endl ;} ~ MC < T >( void ){if(List) killemAll (); cout << "MC dtor" << endl ;} MC < T >* create ( void ){if(!List){List=new MC < T >;List-> left =List;List-> right =List; cout << "List created" << endl ;return List;}else cout << "Listtype already created" << endl ;return 0 ;} //create List MC < T >* push ( void ){ if(!List){List= create ();} MC < T >* newnode =new MC < T >; newnode -> obj =new T ; newnode -> left =List; newnode -> right =List-> right ; List-> right = newnode ; newnode -> right -> left = newnode ; newnode -> count = count ++; end = newnode ; return newnode ; } //push new Object void show ( void ){ MC < T >* iter =List; while(List !=( iter = iter -> right )){ cout << iter -> obj -> GetVal ()<< endl ;} } //show entire List template <class Ty > void setVal (const int & pos ,const Ty val ){ if( pos > end -> count ){ cout << "MC<T>::SetVal() -> Pos : " << pos << " with Val : " << val << " rejected,no such Position!" << endl ;return;} MC < T >* iter =List; while(List != ( iter = iter -> right )){ if( iter -> count == pos ){ iter -> obj -> SetVal ( val );} } } //set Value void killemAll ( void ){ MC < T >* iter =List; while(List !=( iter = iter -> left )){ iter -> right -> left = iter -> left ; iter -> left -> right = iter -> right ; MC < T >* temp = iter -> left ; delete iter ; iter = temp ; } delete iter ; } //delete List T * operator ->( void ){return obj ;} //access operator }; int main ( void ){ MC < Test > test ; for( int i = 0 ; i < 5 ; i ++){ test . push (); test . setVal ( i , i ); } test . setVal ( 100 , 1 ); test . show (); }
Hi @all,
ich hab mir mal eine zugegeben recht einfache Listenklasse gebastelt.
Sinn und Zweck des ganzen ist aus der Klasse MC<T> heraus Objekte dynamisch zu erstellen
und direkt in der Liste zu verwalten.
Sinnvolle! Kritik erwünscht.