Tutorial Object Array
-
Anyone point me to a good tutorial for Object Array List with Serialization? Many thanks
AF Pilot
-
Anyone point me to a good tutorial for Object Array List with Serialization? Many thanks
AF Pilot
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 #include 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. Blai
-
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 #include 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. Blai
Thanks very much for your assistance!
AF Pilot