Archived

This topic is now archived and is closed to further replies.

Bullmax

Linked Classes : Derivation

Recommended Posts

I have this kind of structure in most of my classes : example
  
class CSprite
{
public:
// First part //

CSprite(void);
~CSprite(void);
/*... other methods ...*/
/*... other members ...*/
//

// Second Part //

void Link(void);
void Unlink(void);
CSprite* m_pNext;
static CSprite* m_pList;
}
// Initialization of static member //

CSprite::m_pList = NULL;
  
The first part contains the main methods and properties of the class and the second part contain the "add-on" to link it with classes of the same type. Link() is used to "connect" the object to the current list (m_pList is the beginning of it) Unlink() is used to "unconnect" the object from the current list and close it if he is the only one in it. m_pNext is the pointer to the next element of the list. If it it NULL and it is connected, it may be the last item of the CSprite list. m_pList is the pointer of the beginning of the list. It means it points to the first CSprite element of the list. And now, the problem.... I''ve discovered that this linkage was very usefull and a lot of my classes use it, but each time, I have to rewrite the whole Link, Unlink methods to be adjusted to the used class and rewrite the m_pNext and m_pList members again. I looked at derivation by using something like "class CSprite : public CLinker" by having a linker class with all those elements, but there is a static member and the pointers must be void to store any type of references, no ? It is very confusing... I think I need help... Thanks a lot
/* Bullmax */ ------------- Reality has many forms : good and evil, black and white, ying and yang.

Share this post


Link to post
Share on other sites
Why write your own code to do this when the standard library includes collections written for that very purpose by the world's best computer scientists? If, for example, you want a linked list, then use std::list:

      
#include <list>

class CSprite
{
public:
// First part //

CSprite(void);
~CSprite(void);
/*... other methods ...*/
/*... other members ...*/

//

}
CSprite::m_pList = NULL;

std::list<CSprite> sprite_list;


No need to make life hard for yourself.

--
Very simple ideas lie within the reach only of complex minds.

Edited by - SabreMan on February 16, 2002 7:44:36 AM

Share this post


Link to post
Share on other sites
Woah... Thanks a lot

You''re right this is kinda simpler. But what is this ? I''ve never seen that, are these templates or something else ?

Anyway, that will help, but I should find documentation about it to take the best of it. Do you think I can find it in the Microsoft SDK ?

By the way thanks !



/* Bullmax */
-------------
Reality has many forms : good and evil, black and white, ying and yang.

Share this post


Link to post
Share on other sites
quote:
Original post by Bullmax
Woah... Thanks a lot

You''re right this is kinda simpler. But what is this ? I''ve never seen that, are these templates or something else ?



Yep. It''s the Standard Template Library. Every C++ programmer should know about it, but the trouble is that all these game programming books teach pretty awful C++ programming style (if that''s what you''ve been using).

quote:

Anyway, that will help, but I should find documentation about it to take the best of it. Do you think I can find it in the Microsoft SDK ?


It''s in MSDN, but that''s hardly the best of references. Nicolai Josuttis''s The C++ Standard Library is *the* best tutorial and reference.

--
Very simple ideas lie within the reach only of complex minds.

Share this post


Link to post
Share on other sites
quote:

Nicolai Josuttis''s The C++ Standard Library is *the* best tutorial and reference.



I second that. It''s the most worn book on my bookshelf right now.

I just picked up Meyer''s "Effective STL", but have only skimmed through it so far. His other "effective" books have been really good, so I have high hopes for this one.

Take care,
Bill

Share this post


Link to post
Share on other sites
The effective STL book is excellent, IMHO. Requires you to already know STL, but teaches you many things you did not know.

Share this post


Link to post
Share on other sites
quote:
Original post by Siebharinn
I second that. It's the most worn book on my bookshelf right now.


No point to this post other than I wanted to go on about how good Josuttis's book is. There are 2 books that never leave my desk at work: that one, and Stroustrup's "bible". I'm considering buying second copies of both of them to have them at home too, they're that good. Josuttis's book is sublime. It's so well thought out: the index is so incredibly accurate and informative that I can find pretty much anything about the Standard Library straight away. The writing style is such that I can understand even the more complex parts of the library in ten minutes flat.

I suppose what I'm saying is, sure there are other books and there are websites that contain the same information. Josuttis's book, though, is so good that every C++ developer owes themself a copy.

--
Very simple ideas lie within the reach only of complex minds.

Edited by - SabreMan on February 16, 2002 3:02:50 PM

Share this post


Link to post
Share on other sites
Thanks a lot everyone !

I think I''ll buy it.





/* Bullmax */
-------------
Reality has many forms : good and evil, black and white, ying and yang.

Share this post


Link to post
Share on other sites