I am relatively new to C++ and I have been practicing it for a month since i have a University course, I want to expand my knowledge and learn as much as I can, so that I can write good code. We had an assignment in which we were supposed to create three classes, in the third class we were supposed to write in and group objects from the second class and then do stuff with them, in other words we were asked to create a class named pilot, plane, and then fleet which is made up of planes. The teachers gave this solution and it was quite far fetched from what i envisioned, take a look:
class Fleet {
private:
struct PlaneList {
Plane* p;
PlaneList* next;
PlaneList* node = nullptr , *next_node = nullptr;
public:
void PlaneAdd(Plane* current) {
if (node == nullptr) {
node = new PlaneList;
node->p = current;
node->next = nullptr;
next_node = node;
}
else next_node->next = new PlaneList;
next_node = node->next;
next_node->p = current;
next_node->next = nullptr;
}
};
Now as far as I've gathered we created a list which contains pointers which point to the co-responding plane which is in another class? Also the memory allocation is super weird, i don't really understand it and when I paste this code into my MSVC, the program compiles but it crashes with the message saying that next_node = node->next; is a NULLPTR and it has an access violation? Why am i getting this error? I am totally confused and I'd really appreciate if you could explain it a bit to me. Also I've read that using lists is a terrible performance hit and that using something like vectors is much more efficient.