![](/img/trans.png)
[英]Array of Pointers to an Abstract Class: to nullptr or not to nullptr (C++)
[英]Setting all elements of an array of pointers to nullptr in C++
我只是想知道是否有一種不使用循環就將指向所有空值的指針初始化數組設置的方法嗎?
class Abc{
//An array of 2000 Product pointers
Product* product_[2000];
public:
Abc();
}
我想在調用構造函數時將所有指針設置為null:
Abc::Abc(){
product_ = {};
}
這不起作用,product_必須為可修改的值。 有沒有比循環2000個元素更簡單的方法?
謝謝。
您可以使用:
class Abc{
//An array of 2000 Product pointers
Product* product_[2000];
public:
Abc() : product_{} {}
};
如果您使用std :: array,則默認情況下會將其初始化為nullptr。
std::array<Product *, 2000> product;
使用Visual Studio編譯器,您可以在初始化器列表中將指向NULL的指針初始化,如下所示:
class Abc{
//An array of 2000 Product pointers
Product* product_[2000];
public:
Abc():product_(){};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.