Click here to Skip to main content
15,893,486 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Multiple Windows Threads Pin
Mark Salsbery8-Nov-08 14:46
Mark Salsbery8-Nov-08 14:46 
GeneralRe: Multiple Windows Threads Pin
Bram van Kampen16-Nov-08 13:21
Bram van Kampen16-Nov-08 13:21 
Question2D Arrays Pin
livin lie6-Nov-08 11:47
livin lie6-Nov-08 11:47 
AnswerRe: 2D Arrays Pin
Saurabh.Garg6-Nov-08 15:16
Saurabh.Garg6-Nov-08 15:16 
QuestionRe: 2D Arrays Pin
David Crow6-Nov-08 16:45
David Crow6-Nov-08 16:45 
AnswerRe: 2D Arrays Pin
livin lie7-Nov-08 6:21
livin lie7-Nov-08 6:21 
QuestionTutorial Object Array Pin
Reagan Conservative6-Nov-08 9:02
Reagan Conservative6-Nov-08 9:02 
AnswerRe: Tutorial Object Array Pin
sashoalm6-Nov-08 10:18
sashoalm6-Nov-08 10:18 
http://www.tenouk.com/visualcplusmfc/visualcplusmfc10b.html[^]

One of the easiest ways to use a CObList object is to add new elements to the tail, or bottom, of the list and to remove elements from the head, or top, of the list. The first element added to the list will always be the first element removed from the head of the list. Suppose you're working with element objects of class CAction, which is your own custom class derived from CObject. A command-line program that puts five elements into a list and then retrieves them in the same sequence is shown here:

#include <afx.h>
#include <afxcoll.h>

class CAction : public CObject
{
private:
int m_nTime;
public:
// Constructor stores integer time value
CAction(int nTime) { m_nTime = nTime; }
void PrintTime() { TRACE("time = %d\n", m_nTime); }
};

int main()
{
CAction* pAction;
// action list constructed on stack
CObList actionList;
int i;

// inserts action objects in sequence {0, 1, 2, 3, 4}
for (i = 0; i < 5; i++)
{
pAction = new CAction(i);
// no cast necessary for pAction
actionList.AddTail(pAction);
}

// retrieves and removes action objects
// in sequence {0, 1, 2, 3, 4}
while (!actionList.IsEmpty())
{
// cast required for return value
pAction = (CAction*) actionList.RemoveHead();
pAction->PrintTime();
delete pAction;
}

return 0;
}

Here's what's going on in the program. First a CObList object, actionList, is constructed. Then the CObList::AddTail member function inserts pointers to newly constructed CAction objects. No casting is necessary for pAction because AddTail() takes a CObject pointer parameter and pAction is a pointer to a derived class. Next the CAction object pointers are removed from the list of the objects deleted. A cast is necessary for the returned value of RemoveHead() because RemoveHead() returns a CObject pointer that is higher in the class hierarchy than CAction. When you remove an object pointer from a collection, the object is not automatically deleted. The delete statement is necessary for deleting the CAction objects.

There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal

GeneralRe: Tutorial Object Array Pin
Reagan Conservative6-Nov-08 11:22
Reagan Conservative6-Nov-08 11:22 
QuestionI need help with my program, new c++ programmer Pin
OverdoS6-Nov-08 7:56
OverdoS6-Nov-08 7:56 
QuestionRe: I need help with my program, new c++ programmer Pin
David Crow6-Nov-08 8:17
David Crow6-Nov-08 8:17 
AnswerRe: I need help with my program, new c++ programmer Pin
CPallini6-Nov-08 10:30
mveCPallini6-Nov-08 10:30 
AnswerRe: I need help with my program, new c++ programmer Pin
led mike6-Nov-08 8:52
led mike6-Nov-08 8:52 
QuestionShellExecute/Browser problem Pin
Leslie Sanford6-Nov-08 6:19
Leslie Sanford6-Nov-08 6:19 
AnswerRe: ShellExecute/Browser problem Pin
Renjith Ramachandran6-Nov-08 7:51
Renjith Ramachandran6-Nov-08 7:51 
AnswerRe: ShellExecute/Browser problem Pin
Bram van Kampen6-Nov-08 15:16
Bram van Kampen6-Nov-08 15:16 
QuestionAdvice needed for a tool for c++ code analysis Pin
Renjith Ramachandran6-Nov-08 5:23
Renjith Ramachandran6-Nov-08 5:23 
AnswerRe: Advice needed for a tool for c++ code analysis Pin
David Crow6-Nov-08 7:22
David Crow6-Nov-08 7:22 
QuestionRe: Advice needed for a tool for c++ code analysis Pin
sashoalm6-Nov-08 8:04
sashoalm6-Nov-08 8:04 
AnswerRe: Advice needed for a tool for c++ code analysis Pin
David Crow6-Nov-08 8:09
David Crow6-Nov-08 8:09 
GeneralRe: Advice needed for a tool for c++ code analysis Pin
sashoalm6-Nov-08 10:12
sashoalm6-Nov-08 10:12 
AnswerRe: Advice needed for a tool for c++ code analysis Pin
BonshatS6-Nov-08 8:19
BonshatS6-Nov-08 8:19 
GeneralRe: Advice needed for a tool for c++ code analysis Pin
Renjith Ramachandran6-Nov-08 9:15
Renjith Ramachandran6-Nov-08 9:15 
QuestionHelp with selecting Visual Studio option Pin
Geurt Bloem6-Nov-08 3:58
Geurt Bloem6-Nov-08 3:58 
QuestionRe: Help with selecting Visual Studio option Pin
David Crow6-Nov-08 5:05
David Crow6-Nov-08 5:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.