MyClass* obj;
obj = &MyClass(1);
obj++;
obj = &MyClass(2);
obj++;
...
When I run this, the first obj (obj[0]) is the only one that ever gets changed and it gets overwritten with the other calls; the others are empty.
Pointers in a list not having correct data
How can I make this work:
That's because you're only updating the pointer's address and not the data it's pointing to.
Try this instead:
Try this instead:
*obj++ = MyClass(1);*obj++ = MyClass(2);
Oh, and don't forget to allocate some space for the array somewhere.
The code you've posted doesn't make any sense. What is it that you are trying to accomplish?
Holy something!
Sfpiano: Those objects are temporary, they don't exist any more after that line of code finishs. Using their address for anything is bad.
doynax: obj was never allocated, trying to assign stuff to it is also bad. Nevermind, you commented on that.
I'm going to agree and say that that this doesn't make a whole lot of sense.
Sfpiano: Those objects are temporary, they don't exist any more after that line of code finishs. Using their address for anything is bad.
MyClass* obj; // obj is a pointer. Right now, it's pointing to some random location in memory.// Set's obj to the address of a temporary MyClass object.obj = &MyClass(1);// At this point, that temporary object doesn't exist any more, and obj is pointing to garbage.obj++; // This moves obj forward in memory by the same number of bytes as a MyClass object had. Not only is this not a valid object, but this was _never_ at any point a valid MyClass object.// This sets obj to another temporary object. It could very easily have the same address as the last one. Since you're now setting the address, why the heck you you set the pointer if you just incrimented it? What's the point?obj = &MyClass(2);// Again, obj is pointing to garbage.obj++; // And again, this doesn't make any sense in the context of this code.
doynax: obj was never allocated, trying to assign stuff to it is also bad. Nevermind, you commented on that.
I'm going to agree and say that that this doesn't make a whole lot of sense.
yea, I think you are trying for something like this:
if you're looking for a variable amount of objects to be used in your objects list, look into the STL list or STL vector classes. If you are not already familliar with the concecpt of linked lists, I'd suggest looking it up and writing your own for the learning process.
-me
MyClass *objects[20];for (int i = 0; i < 20; ++i ){ objects = new Myclass(i);}//do some stuff with your objects//absolutely make sure to free the memory you've allocatedfor (int i = 0; i < 20; ++i ){ delete objects;}
if you're looking for a variable amount of objects to be used in your objects list, look into the STL list or STL vector classes. If you are not already familliar with the concecpt of linked lists, I'd suggest looking it up and writing your own for the learning process.
-me
Maybe you should give us information as to *why* you need to do what you're doing. This way we could help you accordingly. Because right now, as people said, your code doesn't make much sense, so we really don't know what to tell you.
Just a thought.
Just a thought.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement