You are struggling with sequence of events. For example, on lines 14..18, the first thing you do is return from the function. Nothing else in the function will happen. (Crank up the warnings on your compiler and it will complain to you about that.)
- The constructor happens just once, when you create the object.
- Likewise, the constructor happens just once, when the object is destroyed.
- The operator[] will happen whenever you use it, which can only be after the constructor runs and before the destructor runs.
As a result, 'A' will have some random value when the constructor runs. The constructor's purpose is to give all the class variables a value before they are used.
It is not clear, but what exactly are you trying to accomplish? (What do you mean by an "automatic" run time array?)
Do you mean that you want the array to grow automatically when the user accesses an element?
For example, after construction, the array has zero elements.
User assigns a value to element at index 2.
Array now has 3 elements (indexed 0, 1, and 2).
?
What I was trying to accomplish by an automatic run time array was that after I called my operator and gave it a value it'd then do all of that inside the constructor. For instance, display all of the indexes and values and the size of the array. However now I know you can't do that since it only runs once. What I tried was this, however it doesn't really work...
Wow, I'm embarassed now haha. Well then, I thought that x[go] would set my array to the value length of that value, I now know i was wrong. I thought it was actually working when it wasn't xD, since all I was printing was garbage if i entered a value of which go was bigger than the one it was set to in the constructor. I changed it now though, I believe this is correct. Sorry for all the weird things/errors in the program. I thought include guards were necessary? Now that I think about those reference arguments I have no idea what I was thinking if I was thinking. I've been trying to learn/use classes and structs and so right now I'm noob at them. Man I completely misunderstood what I was trying to do with the int& operator. It was actually setting a certain value for an index in my array :P like you said. I was confused when you said that but I think I got it now.
Whoa wait a second, this was only working on cpp.sh but not on c9 or visual studio..... What could be wrong I thought it was right?
For one thing on vs or c9 it gives me -842150451 or something and then when I try to exit the program it gives heap corruption detected error. CRT detected that the application wrote to memory after end of heap buffer in vs not c9. What have I done O.o
I think it is something so that you don't introduce the same struct twice or something within a file. I thought I was using them correctly?
Include guards are usually used to prevent the contents of a .h file from being included multiple times. You have used them in a .cpp file. You would not want to include this .cpp in other programs, so include guards within a .cpp file are superfluous.
And as I pointed out earlier, yours are wrong. if bob_s were already defined, your include would effectively look like this:
1 2 3
struct bob {
// #ifndef through #endif skipped
// note: no closure on the structure body
Line 43: Allocates an array of size a. Let's assume a is 10.
Line 44: Assigns a to k[a]. This is an out of bounds reference. Rewriting line 44: k[10] = 10;
You allocated 10 elements [0-9]. Element [10] is out of bounds.
Line 21: You're still printing uninitialized elements.
All it's supposed to do is make me a dynamic array to whatever I want :p. Then print out all the values and the amount of memory in bytes and then delete it when i'm done. I know it's something simple and doesn't need a class. I just made it in a class so that I could use it anywhere without having to copy and paste and be able to use it anywhere. This is the final version as it works with both VS and c9...