mardi 31 août 2021

Does the use of {} in a class member variable std::array result in initializing the array elements? [duplicate]

I have a class X with an std::array member variable:

class X
{
   public:
     //some code here

   private:
     std::size_t var1_;
     std::array<std::uint8_t, 100> var2_; //this is the class member variable
}

I want to ensure that var1_ and var2_ are always initialized even if I do not explicitly initialize them in the constructor. If I do something like this:

  class X
    {
       public:
         //some code here
    
       private:
         std::size_t var1_{0};
         std::array<std::uint8_t, 100> var2_{}; //this is the class member variable
    }

it will initialize var1_ to zero. Does the use of {} for var2_ guarantee that all the uint8_t array elements will be initialized to zero? Also if the use of {} ensures that all the array elements are initialized to zero, is there a way to initialize them to some value other than 0, at the line where var2_ is defined? (i.e., without doing it explicitly in the constructor). I am using C++11. Thanks

Aucun commentaire:

Enregistrer un commentaire