STL List of object pointers how do i call a member function

cosmowen
cosmowen used Ask the Experts™
on
Hello all,
I have a question (obviously) ok i have to write a program for class that uses MFC and STL.  I create a list and list iterator of

CMyClass
{
   int nValue;
   void func1(int, int);
};

CMyClass:://(...)

list<CMyClass*> items;
list<CMyClass*>::iterator iter;

now how do i call the function in this case ?
i want to call the func1() of an
//iter.func1(nVal1, nVal2); //i know this isn't right but u get the idea.
 please help this is due tom. and i have been struggling with it for two weeks now.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
iter is just pointer to a position in your list.

so you need to figure out how to dereference the iter pointer properly to access the actual pointer to your class.

It's a bit confusing, because really you need to imagine it as a double pointer to get access to your actual func1().

imagine that iter looks like this:

CMyClass** iter;

how would you do the actual function call from a double pointer?

Chase
Commented:
the crude way would be

for (iter = item.begin(); iter != item.end(); iter++)
{  
   (*iter)->func1(nval1, nval2);
}

Commented:
Kimpan posted method is the standard method, but if you don't like that type of syntax, you can do the following:

for (iter = item.begin(); iter != item.end(); iter++)
{  
  CMyClass* Ptr = *iter;
  Ptr->func1(nval1, nval2);
}
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Author

Commented:
due to the high point value of my question is there any chance that u know MFC programming?? Kimpan i would like to ask another question but i am awarding u the points

Author

Commented:
thanks and let me know if u know what i need

Commented:
MFC? What is that? A new kind of a bug? :)~

Please, consider WTL as an alternative to MFC in your next project.

Thanks for the points but as Scott Meyer suggested in his book, you might consider not to explicitly write the for-loop for iterating over all elements in your container.

Perhaps you could apply STL algorithm, for_each for this case.

Commented:
Axter, I am a standard guy :)

Commented:
cosmowen, just ask me about MFC, maybe I can help.

Commented:
>>Thanks for the points but as Scott Meyer suggested in
>>his book, you might consider not to explicitly write
>>the for-loop for iterating over all elements in your
>>container.

That is a good point, because if you need to use the same code for a different type, you don't have to modify the code.  
In other words, your code is more generic and more reusable by not including the type.

Author

Commented:
MFC is required in the class as it is a class about MFC
ok here is my problem i have no points to give for this one but as soon as i get some i will ask some simple question and whoever answers me here can have those points.

ok in a CScrollView/CDocument architecture i have an STL::List of class pointers (as gathered in the previous question) now what i need to do is write them on the document and then display them on the screen if u need the program specs to really know what i am talking about goto www.cs.okstate.edu/cs3373 and click the drop-down list on the right, choose assignments and it is program4.
 so anyway, i need to pass the list from the CDocument to the CScrollView class and call it in the OnDraw() method of the CScrollView class.  
This would be easy enough but the whole when is stff created and that crap plus it only needs to display when the user pushes a button that says "Display Text" ??
thanx cosmowen

Author

Commented:
the website addy to the actual program is www.cs.okstate.edu/cs3373/programs/pgm04.html
hope that helps it seems the home page is down.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial