is it possible to return an array from a function ?

 

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?

 
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.
 
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 ?
 
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.

 
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.

 
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]);
 
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.
 
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]));
  }
 
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();
 
Ex Ovo Omnia:

I am often using simple wrappers for arrays.

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

Nice one. How do you extract the array values from such? I'm still learning. 

 
Nelson Wanyama: Nice one. How do you extract the array values from such? I'm still learning.
class DoubleArrayWrapper {
public:

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

DoubleArrayWrapper* w = new DoubleArrayWrapper();

w.value[index] plus you are now responsible for releasing the class on unload (but as written, not on deinit/init EA cycles.) Avoid!

Reason: