Oh really? I didn't know you could do that. I might have to use that.M_D_K wrote:Not really you'd still be using an std::list just accessing it like an array.JS Lemming wrote: An array? That's soo..... I dunno.... I've always had trouble with these dang lists in the past and I'm determined to finally get them working.
Difficulty accessing a member of a class from a list
Moderator: Coders of Rage
- JS Lemming
- Game Developer
- Posts: 2383
- Joined: Fri May 21, 2004 4:09 pm
- Location: C:\CON\CON
Re: Difficulty accessing a member of a class from a list
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Re: Difficulty accessing a member of a class from a list
M_D_K wrote:Not really you'd still be using an std::list just accessing it like an array.
Please excuse my ignorance, but as far as I know you can't access a linked list like an array. There's no guarantee that the elements in a list will share neighboring addresses in memory.JS Lemming wrote:Oh really? I didn't know you could do that. I might have to use that.
Under the hood the STL list should be working basically like in this link:
http://www.codeproject.com/KB/cpp/linked_list.aspx
I apologize if I'm wrong, but I'm pretty sure you can't access an element of a linked list with the [] operator.
- M_D_K
- Chaos Rift Demigod
- Posts: 1087
- Joined: Tue Oct 28, 2008 10:33 am
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/++
- Location: UK
Re: Difficulty accessing a member of a class from a list
Oops.andrew wrote:M_D_K wrote:Not really you'd still be using an std::list just accessing it like an array.Please excuse my ignorance, but as far as I know you can't access a linked list like an array. There's no guarantee that the elements in a list will share neighboring addresses in memory.JS Lemming wrote:Oh really? I didn't know you could do that. I might have to use that.
Under the hood the STL list should be working basically like in this link:
http://www.codeproject.com/KB/cpp/linked_list.aspx
I apologize if I'm wrong, but I'm pretty sure you can't access an element of a linked list with the [] operator.
Yeah std::list doesn't have a [ ] operator. So yeah my bad.
But theres always std::vector
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
-
- Respected Programmer
- Posts: 387
- Joined: Fri Dec 19, 2008 3:33 pm
- Location: Dallas
- Contact:
Re: Difficulty accessing a member of a class from a list
std::list is a pretty literal interpretation of a textbook doubly linked list structure. An iterator is just a container that acts like a pointer which ensures identical operation across all standard complex types: vector, list, map..etc.
So if we presume an iterator as a pointer which marches along the list, then it is valid from a range list::begin() to list.end(). A linked list's memory locality is not guaranteed to be contiguous like an array is because memory for individual elements is allocated off the heap. Typically, every attempt is made to ensure that large allocations are contiguously allocated, however appending and removing elements from either side of the list would spell out retarded overhead from realloc calls on potentially large data sets. So a typical implementation of the [] operator (which is: baseptr + offset * sizeof(datum) ) is not possible in this case. Therefore std::list will not have that operator defined.
(All pseudocode)
So then in this case, since you're dealing with data as follows:
std::list<data*> myList; // Linked list of ptrs
Then the iterator's type becomes: data**
Since list.begin() returns &list.first then we can see how the return value becomes data** since data** = &data*;
So de-referencing the iterator simply yields the object that it references, which is of type data*.
So if we presume an iterator as a pointer which marches along the list, then it is valid from a range list::begin() to list.end(). A linked list's memory locality is not guaranteed to be contiguous like an array is because memory for individual elements is allocated off the heap. Typically, every attempt is made to ensure that large allocations are contiguously allocated, however appending and removing elements from either side of the list would spell out retarded overhead from realloc calls on potentially large data sets. So a typical implementation of the [] operator (which is: baseptr + offset * sizeof(datum) ) is not possible in this case. Therefore std::list will not have that operator defined.
(All pseudocode)
So then in this case, since you're dealing with data as follows:
std::list<data*> myList; // Linked list of ptrs
Then the iterator's type becomes: data**
Since list.begin() returns &list.first then we can see how the return value becomes data** since data** = &data*;
So de-referencing the iterator simply yields the object that it references, which is of type data*.
Re: Difficulty accessing a member of a class from a list
Yeah, there's no way that'll work. =PI did...
Code: Select all
gameStage->(*i)->x = 44;
You're trying to dereference a member as it's own entity!
If we have Object O with member M, can we do this?
O.m=<value>? Yes.
How about O=<value>? If you've overloaded it, sure.
Now, try m=<value>. No, m is a member...edit: Eh, bad example, m could be static...Though you'd still need scope operator...I tried. ;P
So when you do
Code: Select all
gameStage->(*i)->x = 44;
Code: Select all
*(gameStage->i)->x = 44;
Have you tried this:When that class is constructed it makes a hector and puts it in the list...
Code: Select all
#include "stage.h"
#include "hector.h"
stage::stage()
{
hector *dude = new hector;
stageObjects.push_back(dude);
}
EDIT: ^^ btw, does anyone know of a nicer way of doing that so it's all on one line... and not needing of dude?
Code: Select all
stageObjects.push_back((new hector));
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
- JS Lemming
- Game Developer
- Posts: 2383
- Joined: Fri May 21, 2004 4:09 pm
- Location: C:\CON\CON
Re: Difficulty accessing a member of a class from a list
Informative
Small girl at the harbor wrote:Look Brandon, that crab's got ham!