Method of Type Array


is there any way to return arrays from functions? (Like this, not with any
star operators)


class Temp{

public:
static float getNewArray[5](){
return float[5];
}

};

int main(){

float arr[] = Temp::getNewArray();

}
SORRY!!!
Last edited on
I said I DID NOT need the star operator, how ever that is still awesome, any idea without star operator?
When you say "star operator", I'm assuming you mean "pointer"?

Why don't you want to use a pointer? That's exactly what you should use in this situation.
The C++ way is to not use an array, but use a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Temp
{
  public:
  static vector<float>getNewVector()
  {
    return vector<float>(5);
  }
};

int main()
{
  vector<float> vec = Temp::getNewVector();
} 
This does not seem to have any "stars":
1
2
3
4
5
#include <vector>

int main(){
  std::vector<float> arr (5);
}

The 'arr' is effectively an array of 5 floats.

It is an object too, that can be returned from function (a copy/move does occur):
1
2
3
4
5
6
7
8
9
#include <vector>

std::vector<float> bar( size_t n ) {
  return std::vector<float>( n );
}

int main() {
  auto foo = bar( 42 );
}

Topic archived. No new replies allowed.