0

I am not sure how to initialize a map and a dynamically allocated array members of a class in the default constructor. Is there a standard way of doing this? Like, should I initialize the map/dynamically allocated array to 0's or should I not even initialize them at all ?

My could would look something like this:

class A{
   public:
     map<string, int> mapA;
     int* intArray;
}
A::A(){
   map<string, int> mapA;
   intArray = new int[30];
}

But I am finding all sort of troubles when working with this class. What would be the correct way of solving this problem?

Thanks for your help!

urpi
  • 289
  • 1
  • 2
  • 8
  • For ints/doubles/strings I understand there is a problem with the link you posted, but my question is more related to should I initialize the list/map to zeros or should I not do it at all? The question might have been too vague, I fixed it now. – urpi Aug 03 '22 at 03:03
  • `mapA` is already default-constructed. You do not need to do anything. The exception is if you want non-default construction, in which case you'd put that in the initializer list. – paddy Aug 03 '22 at 03:03
  • `std::map` is default constructed to an empty map (map with no keys/values). – wohlstad Aug 03 '22 at 03:04

0 Answers0