• Advertisement
Sign in to follow this  

I want to do a Template... but how?

This topic is 3355 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, that it, i just wanna make a simple template for some code i reuse often, like liked list, binary tree and such... here a sample of a double linked list ill use as an exaple that id'like to implement as a template: an example header file:
#ifndef _C_EXAMPLE_H_
#define _C_EXAMPLE_H_
#ifdef __cplusplus

#define WIN32_LEAN_AND_MEAN // Trim libraies.
#define VC_LEANMEAN         // Trim even further.

#include "Windows.h"
#include "Stdio.h"

#include "SafeKill.h"       // A simple class to delete allocated memory safely


#define ROOT_NODE    -1

//----------------------------------------------------------------------//
// The base structure of the nodes
//----------------------------------------------------------------------//
struct CNode {
	UINT   Index;            // The Root node will always have the index -1
                                 
	//***********************//
        // This is the part id like to be able to put anything(of any type) here
        <Custom data>

example: char   Buf[MAX_PATH]; <=================================
	//***********************//

	struct CNode *PrevNode;
	struct CNode *NextNode;
};

// This code is a class that deal with the nodes
//----------------------------------------------------------------------//
// A Base Class to manage the nodes(so later i could use a derived class and expand it)
//----------------------------------------------------------------------//
class CBaseClass{
public:
	CBaseClass();
	~CBaseClass();
private:
	CNode  RootNode;
	void   InitRootNode();
public:
	UINT   GetNodeCount();
	CNode* GetLastNode();
        
        CNode* GetRootNode();
	CNode* GetNode(UINT Indx);
	
	void PushNode(const char *txt);
	bool PopNode();

	void InsertNode(UINT Indx);
	void DeleteNode(UINT Indx);

	void Clear();
};

#endif
#endif //_C_EXAMPLE_H_
So, using template, how could i change char Buf[MAX_PATH]; to anything like a DWORD for example? The syntax really confuse me lol.

Share this post


Link to post
Share on other sites
Advertisement
Just an example of the template syntax:


template<class T> class MyList
{
private:

class Node
{
public:
Node(const T &t) : data(t),next(0) { }

T data; Node *next;
};

Node *head;

public:
MyList() : head(0) { }

void Add(const T &t);
};

template<class T> void MyList<T>::Add(const T &t)
{
Node *n=new Node(t);

if(!head) head=n;
else
{
Node *i=head;
while(i->next) i=i->next;
i->next=n;
}
}


However, if you really are using this code frequently rather than simply as a learning exercise, you would be better to just use std::list.

As an aside, in C++, prefer const int ROOT_NODE to #define ROOT_NODE and whenever you see char x[MAX_WHATEVER], consider std::string.

And generally, a linked list would not expose an interface to retrieve nodes by index as this is highly inefficient and suggests that a linked list is the wrong container type for the usage. A vector or deque provide efficient random access whereas a linked list is only efficient for iteration but provides efficient insertion and removal of elements.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement