Variabler Rückgabewert



  • Hallo,

    ich habe mir eine Klasse geschrieben, mit welcher ich ein 3-Dimensionales Array beliebiger Größe und beliebigen Datentyps erstellen kann. Nun hätte ich gern eine Methode die mir einen Zeiger auf das erste Element des erzeugten Feldes zurück gibt. Nun ist das erzeugte Feld vom Datentyp her variabel.

    header der Klasse:

    namespace Felder{
    	ref class Dim3Feld
    	{
    
    	public:
    		enum class Datentyp{BOOL,
    						CHAR,
    						usCHAR,
    						INT,
    						usINT,
    						SHORT,
    						LONG,
    						usLONG,
    						FLOAT,
    						DOUBLE,
    						lDOUBLE};
    		Dim3Feld(int d1, int d2, int d3, Datentyp typ);
    		void getZgr();
    
    		private:
    			int dim1;
    			int dim2;
    			int dim3;
    			bool ***boolFeld;
    			char ***charFeld;
    			unsigned char ***usCharFeld;
    			int ***intFeld;
    			unsigned int ***usIntFeld;
    			short ***shortFeld;
    			long ***longFeld;
    			unsigned long ***usLongFeld;
    			float ***floatFeld;
    			double ***doubleFeld;
    			long double ***longDoubleFeld;
    			Datentyp datentyp;
    	};
    }
    

    Konstruktor:

    namespace Felder{
    	Dim3Feld::Dim3Feld(int d1, int d2, int d3, Datentyp typ)
    	  : dim1(d1), dim2(d2), dim3(d3), datentyp(typ){
    		  if(typ == Datentyp::BOOL){
      			  boolFeld = new bool** [dim1];
    			  for(int ix = 0; ix < dim1; ix++){
    				boolFeld[ix] = new bool* [dim2];
    				for(int iy = 0; iy < dim2; iy++)
    					boolFeld[ix][iy] = new bool[dim3];
    			  }
    		  }
    		  else if(typ == Datentyp::CHAR){
      			  charFeld = new char** [dim1];
    			  for(int ix = 0; ix < dim1; ix++){
    				charFeld[ix] = new char* [dim2];
    				for(int iy = 0; iy < dim2; iy++)
    					charFeld[ix][iy] = new char[dim3];
    			  }
    		  }
    		  else if(typ == Datentyp::DOUBLE){
      			  doubleFeld = new double** [dim1];
    			  for(int ix = 0; ix < dim1; ix++){
    				doubleFeld[ix] = new double* [dim2];
    				for(int iy = 0; iy < dim2; iy++)
    					doubleFeld[ix][iy] = new double[dim3];
    			  }
    		  }
    		  else if(typ == Datentyp::FLOAT){
      			  floatFeld = new float** [dim1];
    			  for(int ix = 0; ix < dim1; ix++){
    				floatFeld[ix] = new float* [dim2];
    				for(int iy = 0; iy < dim2; iy++)
    					floatFeld[ix][iy] = new float[dim3];
    			  }
    		  }
    		  else if(typ == Datentyp::INT){
      			  intFeld = new int** [dim1];
    			  for(int ix = 0; ix < dim1; ix++){
    				intFeld[ix] = new int* [dim2];
    				for(int iy = 0; iy < dim2; iy++)
    					intFeld[ix][iy] = new int[dim3];
    			  }
    		  }
    		  else if(typ == Datentyp::lDOUBLE){
      			  longDoubleFeld = new long double** [dim1];
    			  for(int ix = 0; ix < dim1; ix++){
    				longDoubleFeld[ix] = new long double* [dim2];
    				for(int iy = 0; iy < dim2; iy++)
    					longDoubleFeld[ix][iy] = new long double[dim3];
    			  }
    		  }
    		  else if(typ == Datentyp::LONG){
      			  longFeld = new long** [dim1];
    			  for(int ix = 0; ix < dim1; ix++){
    				longFeld[ix] = new long* [dim2];
    				for(int iy = 0; iy < dim2; iy++)
    					longFeld[ix][iy] = new long[dim3];
    			  }
    		  }
    		  else if(typ == Datentyp::SHORT){
      			  shortFeld = new short** [dim1];
    			  for(int ix = 0; ix < dim1; ix++){
    				shortFeld[ix] = new short* [dim2];
    				for(int iy = 0; iy < dim2; iy++)
    					shortFeld[ix][iy] = new short[dim3];
    			  }
    		  }
    		  else if(typ == Datentyp::usCHAR){
      			  usCharFeld = new unsigned char** [dim1];
    			  for(int ix = 0; ix < dim1; ix++){
    				usCharFeld[ix] = new unsigned char* [dim2];
    				for(int iy = 0; iy < dim2; iy++)
    					usCharFeld[ix][iy] = new unsigned char[dim3];
    			  }
    		  }
    		  else if(typ == Datentyp::usINT){
      			  usIntFeld = new unsigned int** [dim1];
    			  for(int ix = 0; ix < dim1; ix++){
    				usIntFeld[ix] = new unsigned int* [dim2];
    				for(int iy = 0; iy < dim2; iy++)
    					usIntFeld[ix][iy] = new unsigned int[dim3];
    			  }
    		  }
    		  else if(typ == Datentyp::usLONG){
     			  usLongFeld = new unsigned long** [dim1];
    			  for(int ix = 0; ix < dim1; ix++){
    				usLongFeld[ix] = new unsigned long* [dim2];
    				for(int iy = 0; iy < dim2; iy++)
    					usLongFeld[ix][iy] = new unsigned long[dim3];
    			  }
    		  }
    	}
    }
    

    Im main erzeug ich jetzt ein Dim3Feld Objekt

    int xBereich = 201;
    int zBereich = 201;
    int tBereich = 400;
    Felder::Dim3Feld^ ausgangsFeld = gcnew Felder::Dim3Feld(xBereich, zBereich, tBereich, Felder::Dim3Feld::Datentyp::FLOAT);
    

    und hätte gerne einen 3-fach Zeiger auf das erste Element des Feldes in der Art:

    float ***aaafFeld = ausgangsFeld->getZgr();
    

    Die Funktion getZgr() hätte nun aber variable Rückgabewerte, in Abhängigkeit der Felder die ich erzeuge.

    Kann mir jemand sagen ob, und wenn wie, es möglich ist eine Funktion mit einem variablen Rückgabewert zu definieren?



  • dreifach Zeiger hab ich persönlich in der Praxis noch nie gesehen. Für was ?!

    Ansonsten: Meinst Du nicht das tempaltes eine bessere Lösung wären ?



  • Das mit den Dreifachzeiger funktioniert schon ganz gut. Muss zur Zeit viel mit 3 Dimensionalen Arrays umgehen und hab die bisher immer im Main angelegt. Das wird aber auf Dauer umständlich und unübersichtlich. Deswegen jetzt halt der Weg über die Klasse.

    Ganz unwissend muss ich jetzt auch mal fragen, was sind tempaltes (oder wahrscheinlich eher templates)?



  • Da hab ich ein wenig zu schnell geantwortet. Ganz grau kann ich mich noch entsinnen. Mal gucken inwieweit mir das hier weiter hilft.





  • Hilft alles nicht so recht. Dann muss ich wohl doch auf die Variante zurückgreifen, jedem möglichen Datentyp eine eigene Rückgabefunktion im Stile von getFloatZgr, getIntZgr, getCharZgr etc. zu geben.


Anmelden zum Antworten