If you’re new to programming, you’re probably thinking iterating through a list of elements is a pretty straightforward task. You’re probably thinking you’ll be able to do it by doing the for loop through each element and then assigning each element a new value. And then you’ll be done, right? Wrong. There’s a lot more to it than that.
In an average c++ program, there are a lot of things that can go wrong. So if youre trying to iterate over a list of elements, you need to understand that there are a lot of things in your program that could cause a crash, and that you need to be able to handle any and all of them. The problem is that if something goes wrong, you can have a very big problem on your hands.
The code below is an example of what I mean. The above code will iterate through the list, and if the list is empty, it will write a message to the console, however if an element isnt there to write to, it will return a default value. The problem we’re running into is that the list is empty and no element is there to write to.
When I use this code it crashes. I think this is because I have defined a class member to be non-overridden. That means that I cant just write the code with a default value, I have to write the code with a value.
Well, in addition to the problem is that I have to define my class member non-overridden, which means I cannot overwrite it, which means I cannot make it private.
The way I see it, if you define a member in a.h file that is not in a.cpp file, you have to either define it in a.cpp file, use a.cpp file to define it, or use a.h file that is in the.cpp file.
That’s why I said making a member private has some downsides. A member can be private in one file, and public in another. In other words, class members can be private in a.cpp and public in a.h, and public members can be private in.cpp and public in.h.
If you’re not using a.h to define a member (or define a member in the.cpp file), then you can just use a.cpp to define it.
You can only define a member in a.h. You can do this because if you define a member in the.h file, you can use it in any.cpp file that has a.h file as a dependency. You could use a.cpp to define it, but that would basically mean that any.cpp file that includes a.cpp would include the.h file.
The idea is to have a.cpp define the value of a.h, and any.cpp file that includes that file should have the definition of the value of that.h. If you dont have a.h file, you have to use a.cpp to define it.