web123456

Assignment method of arrays in C++

Original address is:Assignment method of arrays in C++

existC++, if oneArrayis defined in a class, then itsAssignmentmethodIt is different from the usual process-oriented replication method. When defining an array, you can no longer assign values ​​to the array at the same time, nor can you directly use equations to assign values ​​to the array in the constructor.

One method is as follows:

 1 class A{
2 private :
3 int m_arr[10];
4 public:
5 A()
6 {
7 temp_arr[10] = {1,2,3,4,5,6,7,8,9,10};
8 memcpy(m_arr,temp_arr,sizeof(temp_arr));
9 }
10
11 ....................
12 }

Note that here memcpy belongs.

I feel that the design of array assignment here is too awkward, but object-oriented language is an abstraction of various situations in reality, and this situation is also based on reality.


Please indicate the address of this article when reprinting:Assignment method of arrays in C++