Frage

I am a C++ programmer. I am interested in windows programming and trying to work with visual c++ on windows form. But I am having many problems. The one I am facing right now is very peculiar can be best described through codes.

Inside a Form, I have two variable: one is a List of MyClass and another is of type MySettings.

List<MyClass> NewClass;     // MyClass is value type
MySettings    NewSettings; // MySettings is value type

MySettings holds values of certain properties of MyClass. I need to assign these values to every member of NewClass. So I created a function called Init(). There I tried doing this like this-

for (int i = 0; i < NewClass.Count; i++)
    {

                    /**
                    Checking the value of Cash
                    */

                    int temp = NewClass[i].GetCash(); // temp shows Garbage

                    /**
                    Setting the value of "Cash" in MyClass from NewSettings
                    */ 
                    NewClass[i].SetCash(NewSettings.GetCash());  // Cash is of type System::int32

                    /**
                    Now checking the Cash value after assignment
                    */

                    temp = NewClass[i].GetCash(); // temp shows exact same Garbage
    }

The definition of MyClass-

public value MyClass
{
private:

     // other variables
     String^ Name;
     int Cash;
     .
     .
     .

public:

     MyClass(String^ name)  // constructor
     {
         Name = name;
     }

     .
     .
     .

     int GetCash()
     {
         return Cash;
     }

public:     
     void SetCash(int amount)
     {
         Cash = amount;
     }
}

As the comments mention, I get garbage value of Cash even right after the assignment. In the debug mode, It shows that Cash has indeed changed to passed value but the value is somehow released. What is the reason of this? If my coding is wrong then how can I correct it?

UPDATE

I have edited the constructor and made every int32 property(including Cash) of the MyClass to zero (0). Now temp shows 0 every time.

War es hilfreich?

Lösung

...are you sure that List::operator[] isn't actually returning copies of the objects stored in the list (copy consstructor)? In that case, you are .setCash()ing the copied objects...

put a

public value MyClass
{
   MyClass(const MyClass& a) { std::cout << "CC!" << std::endl; }
}

and find out

Andere Tipps

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):

  1. 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)
  2. you could have already used e.g. SetCash to set it before using it (also not shown in your code), or
  3. 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

  1. 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
  2. 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?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top