Click here to Skip to main content
15,392,636 members
Articles / Desktop Programming / MFC
Posted 2 Apr 2007


17 bookmarked

An Addition to Smart List classes

Rate me:
Please Sign up or sign in to vote.
3.79/5 (18 votes)
22 Jul 2008CPOL
This article presents the code of Simon Hughes’ SmartList with some new functions


This article presents the code of Simon Hughes' SmartList with some new functions that I have added. This code is (as it was) completely free and can be used however you want, but please leave our (Simon's and mine) e-mail addresses in the code to receive possible bug-reports.

This article presents a number of list classes that encapsulate the MFC list classes with some new features. This code (with the new functions) is in some of my projects and has been fully tested. But, if anyone out there finds out any bugs or improvements please send them to me, I will correct them as soon as possible.

The New Functions

BOOL FindAndRemoveElement (const ARG_TYPE searchValue);

BOOL FindAndReplaceElement (const ARG_TYPE searchValue, const ARG_TYPE 

BOOL operator== (const CMyList &temp)

// Find and Remove a TYPE object of the list, searching by ARG_TYPE
template <class TYPE, class ARG_TYPE> 

BOOL CMyList<TYPE, ARG_TYPE>::FindAndRemoveElement(const ARG_TYPE 
    ASSERT_VALID (this); 

    POSITION pos = Find (searchValue); 
    if (pos != NULL)    // When found, remove element 
        return TRUE;
        return FALSE; 

// Find and Replace a TYPE object of the list, searching by ARG_TYPE
template <class TYPE, class ARG_TYPE> 
BOOL CMyList<TYPE, ARG_TYPE>::FindAndReplaceElement(const ARG_TYPE 
    searchValue, const ARG_TYPE newValue) 
    ASSERT_VALID (this); 

    POSITION pos = Find (searchValue); 
    if (pos != NULL)    // When found, replace old element with new one 
        SetAt (pos, newValue); 
        return TRUE;
        return FALSE; 

// Equality operator for the whole list (CMyList1 == CMyList2)
template <class TYPE, class ARG_TYPE> 
BOOL CMyList<TYPE, ARG_TYPE>::operator== (const CMyList &temp) 
    ASSERT_VALID (this);
    ASSERT_VALID (&temp);

    int nMatches = 0; // To have the number of matches

    if(this == &temp) // Check for self assignment
        return TRUE;

    // If one list has different number of elements, can't be equal
    if (GetCount () != temp.GetCount ())
        return FALSE;

    POSITION posThis = GetHeadPosition ();
    POSITION posOther = temp.GetHeadPosition ();
    while ((posThis)&&(posOther))
    {    // This is to look for in the same place in both lists
        TYPE thisTYPE = (TYPE)GetNext(posThis);
        TYPE otherTYPE = (TYPE)temp.GetNext(posOther);

        //This presupposes that TYPE object has implemented the operator==
        if (thisTYPE == otherTYPE)
    // If all the objects in the list were equal… the lists are equal
    if (nMatches == GetCount ()) 
        return TRUE;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Germany Germany
I come from Spain. After making a lot of silly things during the studies, I wanted to correct me and to make something positive with my life, so I asked for (and got) an “Erasmus” scholarship to go to Germany. After obtaining a placement to make my Thesis in a Firm of Automation and Software development, I reached the double Degree in Electronics’ Engineering and Informatic. I worked a time with VC++ 6, after that I was over many years busy with industry PLC and roboter programming. Now I am working on something related to car electronics and production. Some years after, I have come back to my roots with C++ and PLC Programming.

I contrinue trying to learn more C# in my spare time (which is even less than before, God bless my kids)

Comments and Discussions

GeneralMy vote of 5 Pin
Michael Haephrati7-Jun-13 9:00
mvaMichael Haephrati7-Jun-13 9:00 
GeneralRe: My vote of 5 Pin
Nelek8-Jun-13 4:14
protectorNelek8-Jun-13 4:14 
GeneralMy vote of 5 Pin
Michael Haephrati15-Mar-13 2:44
mvaMichael Haephrati15-Mar-13 2:44 
GeneralRe: My vote of 5 Pin
Nelek15-Mar-13 5:33
protectorNelek15-Mar-13 5:33 
GeneralA minor improvement Pin
Steve Mayfield9-Apr-07 16:12
MemberSteve Mayfield9-Apr-07 16:12 
GeneralRe: A minor improvement Pin
Nelek10-Apr-07 1:09
protectorNelek10-Apr-07 1:09 
GeneralLooks useful Pin
Hans Dietrich2-Apr-07 15:47
mentorHans Dietrich2-Apr-07 15:47 
GeneralRe: Looks useful Pin
Goran Mitrovic3-Apr-07 0:48
MemberGoran Mitrovic3-Apr-07 0:48 
GeneralRe: Looks useful Pin
Stephen Hewitt10-Apr-07 14:06
MemberStephen Hewitt10-Apr-07 14:06 
GeneralRe: Looks useful [modified] Pin
Nelek10-Apr-07 19:53
protectorNelek10-Apr-07 19:53 
GeneralRe: Looks useful [modified] Pin
Nelek3-Apr-07 5:22
protectorNelek3-Apr-07 5:22 

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.