Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implementing custom Collection Classes 1

Status
Not open for further replies.

PlasmaZero

Programmer
Dec 6, 2002
13
0
0
US
I've looked around, and can't seem to find a good tutorial for writing custom collection classes. As part of a project involving music, I'm attempting to create a collection of Note objects (Note being a custom class of mine). From what I could understand of the info at msdn.microsoft.com, I created this, but it won't work:

#include <afxtempl.h>

*snip*

class Notes
{
Notes();
~Notes();

public:
void Add(Note note){
noteList.AddHead(Note note);
}

void Remove(int index){
if (index > Count - 1 || index < 0)
throw new Exception("Index out of bounds");
noteList.RemoveAt(noteList.FindIndex(index));
}

void Insert(int index, Note note) {
noteList.InsertBefore(noteList.FindIndex(index), Note note);
}

Note Item(int Index) {
if (!noteList.IsEmpty())
return (Note) noteList.GetAt(noteList.FindIndex(Index));
else
return null;
}

private:
CList<Note, Note> noteList;
};

So, I'd appreciate it if anyone can help me out here. I only need basic functionality really - Add, Remove, Insert, GetAt...

Thanks
 
You should explain what about it won't work. Are there compile errors? What are the compile errors? I see simple syntax mistakes in that code. For example, in your Add function you call AddHead(Note note) instead of AddHead(note). Also, your Item function returns a Note object, so returning null won't work.

I don't see anything wrong with the idea behind your solution. However, another way to go that might be even easier is to just use a typedef:
Code:
typedef CList<Note, Note&> NoteList;
Is there a reason you want a custom collection? If you only need basic functionality, I see no reason not to just use a CList or std::list or a typedef of an existing list.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top