From the code you've posted so far, it's hard to tell why the final value of "temp" is garbage, because it depends on how the "NewSettings" object has been initialized... if you have initialized it properly, the final value of "temp" in each iteration of your "for" loop should be well defined. But given the rest of your question, I'm guessing it may not be.
Specifically, you seem surprised to get garbage from your initial "NewClass[i].GetCash()" call; I'll focus the rest of my answer on that, and hope you can apply it to ensuring that NewSettings has been properly initialized.
There are three basic ways in which the Cash field of your MyClass objects could be properly initialized (and thus not have a garbage value):
- you could have a MyClass constructor that initializes the Cash field when the MyClass objects are created (you don't show one in your code)
- you could have already used e.g. SetCash to set it before using it (also not shown in your code), or
- you could have created the MyClass objects in a way that guarantees default initialization of integers.
I'm guessing that you're familiar with (1) and (2) and are wondering about 3, and why things must be "properly initialized" to avoid a garbage value in the first place.
Note that, since the early days of C, the language has had a focus on letting the (presumably expert) programmer control fine details of efficiency rather than providing "reasonable" behavior for novice C programmers. So, for example, if we have a local variable in a function, the compiler does not have to generate code to initialize it --- this would cause the program to run slightly slower on most architectures. So, for example, if you write this:
void example()
{
int i;
printf("i is %d\n", i);
i++;
printf("i is now %d\n", i);
}
you can get different answers for different calls, as the memory that gets used for "i" may have different things in it at the start of the function. Try putting calls to "example" in between calls to random other things (sqrt, printf, whatever else) in a main function and see what your system does.
Note that you will often find that an integer value is initially 0, a pointer null, etc., because the operating system typically overwrites old values in RAM with 0's before giving them to your program, as a security measure. This feature is built into the C and C++ language definitions for global and static variables, because they are normally going directly into memory that comes fresh from the OS, rather than memory that your program has had for a while and which may have been used for other no-longer-live variables.
So, the answer to your "temp shows garbage" has to do with how the MyClass objects in NewClass are being created ... you could read up on "allocators" in STL lists if you like; I don't know the details myself. But I suspect that you don't actually care about the initial value of "temp", but just put that there during testing, and actually care about the final value of temp, which comes from NewSettings.
If that's the case, the easiest fix is to either
- Provide a constructor for your MySettings class to be sure that NewSettings (and every other MySetting object) has a properly initialized value to hand back from GetCash, or
- Use some other method of MySettings to provide a value for NewSettings before you enter the loop shown in your code.
If the above is all already familiar to you, and perhaps you have initialized NewSettings, then the problem could be some more subtle things with your list class, as other responders have suggested ... I had originally misread your "list" class as the STL list, but when I tried using [ ] to subscript an STL list, it didn't work, so I'm guessing you've got your own list class? If so, can you clarify why you have chosen not to use the standard list class?