简体   繁体   中英

Setting all elements of an array of pointers to nullptr in C++

I am just wondering if there is a way of setting an initialized array of pointers to all null values without using a loop?

class Abc{
  //An array of 2000 Product pointers
  Product* product_[2000];
      public:
         Abc();
}

I want to set all pointers to null when the constructor is called:

Abc::Abc(){
    product_ = {};
}

This does not work, product_ must be a modifiable value. Is there an easier way than looping 2000 elements?

Thanks.

You may use:

class Abc{
  //An array of 2000 Product pointers
  Product* product_[2000];
      public:
         Abc() : product_{} {}
};

如果您使用std :: array,则默认情况下会将其初始化为nullptr。

std::array<Product *, 2000> product;

With Visual Studio compiler you can initialize the pointers to NULL in the initializer list like below-

class Abc{
  //An array of 2000 Product pointers
  Product* product_[2000];
      public:
         Abc():product_(){};
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM