How to directly transfer an array to a function in ? For the example of it would look like this:

void arraySetter(float[] array) { this.array = array; } arraySetter(new float[] {1.2, 2.53, 6.23}); 

In c ++, I do something similar in one of the classes, let it be SomeClass :

 void SetArray(float array[]) { this->array = array; //error: incompatible types in assignment of 'float*' to 'float [0]' } 

And I call (most likely not correctly):

 some_class_pointer->SetArray(new float[5]{2.4, 1, 0.76, 0.54, 0.33}) 
  • and what exactly do you want to do. It’s just that in C , unlike Java , you don’t know the size of an array. Therefore, it makes sense to think about what is necessary. And yes, how did you declare array in the class? if as a float * it is compiled and if as a float[] then this in itself is not very correct and means something else. - pavel
  • @pavel, sizeof(array)/sizeof(array[0]) ? - Herrgott
  • In C ++, built-in arrays are very limited in their capabilities, so it’s more convenient to use std :: vector, which knows its size. - Vladimir Gamalyan
  • @Herrgott this works until the first transfer to the function. In the function type[] converted to type * (you can check). - pavel
  • @vladimir well, can you write an example in response? With the transfer of the array in the raw, so let's say - Herrgott

2 answers 2

Using a vector:

 // Это поле вашего объекта, которое будет хранить массив, передаваемый в SetArrray std::vector<float> Foo::array; // Это метод, который принимает массив для сохранения в поле array void Foo::SetArray(const std::vector<float>& array) { this->array = array; } // Это вызов SetArray который установит массив some_class_pointer->SetArray({2.4, 1, 0.76, 0.54, 0.33}) 

Well, then use the array field, for example, get the first element as array[0] , or the size of the array as array.size() .

As a bonus - no manual new / delete and potential memory leaks.

  • I would still pass on the link ... - pavel
  • @pavel, of course, sealed off - Vladimir Gamalyan
  • @VladimirGamalian when calling SetArray an explicit type indication is superfluous. SetArray is enough ({2.4, 1, 0.76, 0.54, 0.33}) - gbg
  • @gbg is right, I wanted to show that there may be a variable of this type, but yes, probably better update. - Vladimir Gamalyan
  • one
    @VladimirGamalian has nothing to do with the great GDB debugger. - gbg

An array in C ++ 11 is a fixed-size array and a rubber-sized vector .

It is best to pass both arrays by a constant link, if you do not need to modify them:

 void foo(const vector<int>& arr); void foo(const array<int>& arr); 

Or simply by reference, if you need to modify:

 void foo(vector<int>& arr); void foo(array<int>& arr); 

Also, it will be great practice to pass not an array, but a pair of iterators to the beginning and end of the desired range:

 void foo(const vector<int>::iterator& begin,const vector<int>::iterator& end); 

Well, an example of what you tried to do:

 #include <iostream> #include <vector> #include <iterator> using std::vector; using std::ostream_iterator; using std::copy; using std::cout; using std::endl; void foo(const vector<int>& a) { ostream_iterator<int> out_it (cout,", "); copy ( begin(a), end(a), out_it ); cout << endl; } int main() { foo({4,5,6}); return 0; } 

IDEONE

  • 3
    and iterators unless it is safe to transfer? and if a vector extension occurs, they will also become invalid. - pavel
  • I need to transfer elements of different values ​​and different quantities to each object of this type in which the function. This answer seems to be wrong - Herrgott
  • one
    @pavel depends on what you are going to do. If you process data in an array, pass iterators. If you need somewhere to have a pointer to an array, you should make this pointer smart. - gbg
  • @gbg, why begin and end work without std:: ? - pank
  • @SergeyPestov - I'm surprised myself. - gbg