CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2009
    Posts
    23

    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!

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    May 2009
    Posts
    23

    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
  •  





Click Here to Expand Forum to Full Width

Featured