Download MetaTrader 5

is it possible to return an array from a function ?

To add comments, please log in or register
Mohsen Khashei
622
Mohsen Khashei  

and one important question:

is it possible to return an array from a function.

for example :

struct Layer{
...some stuf...
}
class CNet{
private :
Layer m_layers[];
public:
.
.
//I want a getter function for m_layers
const Layer[]& getLayers(const Layer& layers[]) const;

}
//and function implementation:
const Layer[]& CNetwork::getLayers(const Layer& layers[]) const
{
return m_layers;
}

 but it get me several errors . any idea?

Alain Verleyen
Moderator
31203
Alain Verleyen  
pumper:

and one important question:

is it possible to return an array from a function.

for example :

 but it get me several errors . any idea?

No, it's not possible. You have to pass your array as a parameter.
Mohsen Khashei
622
Mohsen Khashei  
so in such a case that shown in the code (class private members) we must change the member to public or there is other ways ?
Alain Verleyen
Moderator
31203
Alain Verleyen  
pumper:
so in such a case that shown in the code (class private members) we must change the member to public or there is other ways ?

If you change to public, there is no point to have get/set methods.

What you need is probably to use a pointer.

Mohsen Khashei
622
Mohsen Khashei  
angevoyageur:

If you change to public, there is no point to have get/set methods.

What you need is probably to use a pointer.

can you explian it clearly.I dont understand how a pointer can access a private class member.

Jose Miguel Soriano
4984
Jose Miguel Soriano  
pumper:

can you explian it clearly.I dont understand how a pointer can access a private class member.

//--------------------------- PRUEBA ARRAY PARÁMETRO ENTRADA-SALIDA ----------------
void prueba(string &arPrueba[])
{
   int k= 0,
       dim= ArraySize(arPrueba);
   for(k= 0; k<dim; k++) arPrueba[k]= "prueba"+DoubleToString(k, 0);
}

//----------------
string array[5];
prueba(array);
for(int k=0; k<5; k++) Print(array[k]);
Mohsen Khashei
622
Mohsen Khashei  
josemiguel1812:
hi jose  thanks for the reply.but this is not the answer .the method you used is for setter part and i did it almost this way.but for getter part i dont know how to do it.
Alain Verleyen
Moderator
31203
Alain Verleyen  
pumper:

can you explian it clearly.I dont understand how a pointer can access a private class member.

You have to transform you Layer struct into a class as pointer can't be used with struct.

class Layer
  {
//...
  };

class CNet
  {
private :
   Layer             m_layers[];
public:
   //.
   //.
   //I want a getter function for m_layers
   Layer            *getLayers(int index);

  };
//and function implementation:
Layer   *CNet::getLayers(int index) 
  {
// todo :index checking
   return(GetPointer(m_layers[index]));
  }
Ex Ovo Omnia
3190
Ex Ovo Omnia  
pumper:

and one important question:

is it possible to return an array from a function.

for example :

 but it get me several errors . any idea?

I am often using simple wrappers for arrays.

Unlike arrays the wrapper object can be both passed to and/or returned from functions.

class DoubleArrayWrapper {
public:

   /** Wrapped array. */
   double value[];
};

DoubleArrayWrapper* w = new DoubleArrayWrapper();
To add comments, please log in or register