I am talking about a large number of assignments to the class member array.

zhaozj2021-02-11  181

Today, QQ, there is a friend asking me how to give a class of member variables [is an array] assignment. For example: Class Ctest {Private: Int m_arr [10]; ......};

What should I do if I want to set the initial value if I want to set the beginning of the member m_arr []?

I tried ctest (): m_arr ({1, 2, 3, 4 ...}) compiled by can't pass, and there is an error: Cannot Specify Explicit Initializer for arrays cannot specify significant initialization to array.

I then told him to assign a value inside the constructor, he said if this ctest () {m_arr [0] = m_arr [1] = ...} is too annoyed. If ctest () {m_arr = {1,24,44, ...};} is compiled again.

I thought, I used this approach:

#include #include #include #include #include

Class ctest {private: int m_arr [10]; public: ctest () {Int Temparr [10] = {1, 2, 3, 4, 6, 6, 7, 5, 8, 2}; Memcpy (M_ARR, Temparr , SIZEOF (Temparr); void disp () {for (int i = 0; i <10; i ) {cout << "m_arr [" << i << "=" << m_arr [i] <

Void main (void) {ctest t; t.disp ();

The result is compiled, the result is correct.

I don't know if you encounter this situation, how do you handle it?

转载请注明原文地址:https://www.9cbs.com/read-5760.html

New Post(0)