|
-
October 4th, 2009, 08:37 AM
#1
STL newbie: calling member functions through lists
Hi,
Iam trying to teach myself STL list containers, i haven't been able to figure out how to call a member function for individual objects in a list, I have a list consisting of objects.
heres the code:
Code:
#include<iostream>
#include<list>
using namespace std;
class test
{
int num;
public:
test() {}
test(int a) { num = a; }
int getnum() { return num;}
};
int main()
{
list<test> tlist;
list<test>::iterator titerator;
tlist.push_back(99);
int i = *titerator.getnum(); //wont compile
return 0;
}
Vectors are a lot simpler, i just use the index and dot operator I have looked quite a few online tutorials, but haven't found one dealing with a situation like this.
Suppose i remove an element from a vector (say myvec[10]), now would myvec[10] be empty(NULL)??? or do elements shift up to fill in the empty space?
help!
-
October 4th, 2009, 08:52 AM
#2
Re: STL newbie: calling member functions through lists
Code:
int main()
{
list<test> tlist;
tlist.push_back(99);
list<test>::iterator titerator = tlist.begin(); // don't let it unitialized!
int i = (*titerator).getnum(); // this does compile.
int j = titerator->getnum(); // this compile also
return 0;
}
Last edited by ovidiucucu; October 4th, 2009 at 08:54 AM.
-
October 4th, 2009, 09:17 AM
#3
Re: STL newbie: calling member functions through lists
Thank you very much
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|