0

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.

2
  • 2
    What have you got against loops? Commented Aug 16, 2017 at 0:16
  • 3
    Abc::Abc() : product_{} {} Commented Aug 16, 2017 at 0:19

3 Answers 3

3

You may use:

class Abc{
  //An array of 2000 Product pointers
  Product* product_[2000];
      public:
         Abc() : product_{} {}
};
Sign up to request clarification or add additional context in comments.

Comments

2

If you use std::array they'll be initialised to nullptr by default.

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

Comments

0

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_(){};
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.