0

I have an array of class objects:

Course* courseList[100]; 

If I wanna set the CourseList to a null pointer, do I do courseList = nullptr; But I get an error for "expression must be a modifiable value.

For a constructor of my class ScmApp

ScmApp::ScmApp() { noOfCourses = 0; courseList_[100] = {}; } 

is this the way to set all elements of courseList_ to nullptr?

9
  • No you need to loop the whole container and assign each element to be a null pointer. When this Course* courseList(100, null); does the same thing. Commented Aug 11, 2016 at 23:41
  • 1
    "If I wanna set the CourseList to a null pointer" CourseList is not a pointer, it is an array of pointers. Do you want to set each element to nullptr? Commented Aug 11, 2016 at 23:43
  • 2
    courseList is an array. You cannot set arrays to null, just like you cannot set cats or dishwashers to null. Only pointers can be null. Commented Aug 11, 2016 at 23:44
  • @KerrekSB, Why wouldn't Course* courseList[100] = {nullptr}; do the job? Commented Aug 11, 2016 at 23:50
  • 2
    You don't have an array of "class objects", you have an array of pointers. Commented Aug 11, 2016 at 23:58

4 Answers 4

3

The only thing you can set to a nullptr is, well, a pointer.

CourseList is not a pointer. That's the problem. That's why you can't set it to a null pointer.

CourseList is an array of pointers. What you can do, instead, is to set every pointer in a CourseList to a null pointer:

for (auto &p:CourseList) p=nullptr; 
Sign up to request clarification or add additional context in comments.

Comments

2

Your CourseList is a group of 100 elements and not just a single element. So you will need to iterate all the 100 and assign each element to be a null pointer. When this Course* courseList[100] = {}; does the thing.

3 Comments

The only thing Course* courseList(100, nullptr); does is fail to compile.
courseList isn't a container, it's an array, that's why there also isn't a constructor taking 2 arguments.
I just checked it and you guys are right. My bad @molbdnilo thanks
2
Course * courseList[100] = {}; 

If you to initialize this array in your constructor use

std::fill(courseList, courseList + 100, nullptr); 

or use std::array<Course *, 100> courseList; instead of Course * courseList[100];

1 Comment

@Andy You got several valid answers for your question. Now did you try it??
0

CourseList* courseList[100] does not create a pointer to an array, it creates an array of pointers. Depending on what you actually want you should be using one of the following

An array of CourseLists

CourseList courseList[100]; // possibly with = {} courseList[0].attr = whatever; // usage 

Better than the above, but similar, a std::array of CourseLists

std::array<CourseList, 100> courseList; // possibly with = {} courseList[0].attr = whatever; 

Or the closest to what it seems you want, a pointer to a std::array of CourseLists. This is probably not best, but nonetheless, here it is:

std::array<CourseList, 100>* courseList = nullptr; courseList = new std::array<CourseList, 100>{}; // prefer unique_ptr courseList->at(0).attr = whatever; 

If you don't know the size at compile time, or want to pass around the object, your best bet is to use a std::vector

std::vector<CourseList> courseList(100); courseList[0].attr = whatever; 

Finally, the weirdest option would be to actually define a pointer to an array:

CourseList (*courseList)[100] = nullptr; 

Addressing the update: you can just use default initializers in your class like so:

class ScmApp { private: CourseList* courseList[100] = {}; int noOfCourses = 0; }; 

But it seems like you should be using a growable container, std::vector and calling push_back to repeatedly add courses. What I see is a very hard-to-maintain keeping track of who is in what class.

If it is absolutely necessary that you take an existing array and set everything to nullptr then you can use std::fill

std::fill(std::begin(courseList), std::end(courseList), nullptr); 

1 Comment

@Andy done, but you should be seriously rethinking your approach.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.