Question:
How do you initialize or assign values to an array in a class?
Answer:
The short answer is that you can’t initialize an array member as you would initialize a scalar data member. You can use memset to zero all the elements of the array inside the constructor’s body or use a loop that assigns values to each array element.
class A{ private: int ar[10]; public: A() { for (int i=0; i<10; ++i) ar[i] = i; }}
In most cases, it's best to use a vector object instead of a built-in array, thereby avoiding the initialization problem right from the start:
class A{private:std::vector < int > vi;//instead of array//...};
Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.






















