Jump to content
  • Advertisement
Sign in to follow this  
Daishim

Inheritence with private internal class

This topic is 4814 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

I'm trying to inherit from a templated list class that contains a private internal class for nodes. My inheritence seems to work just fine, except for the methods of the new class that inherited from the old class cannot use the internal class from the inherited class. I tried making it protected, but I can't seem to get it to work. Am I overlooking something?

Share this post


Link to post
Share on other sites
Advertisement
Templated list
g_list.h

/*
::::::::::::::
:: g_list.h ::
::::::::::::::

Contains the object definition for the engine linked list

Created: June 11, 2005
Last Updated: June 12, 2005
*/

#ifndef G_LIST_H
#define G_LIST_H

#include <vector>
using namespace std;

template <class G_LIST_TEMPLATE>
class G_LIST
{
protected:
// Private node class
virtual class NODE
{
private:
NODE *Next;
NODE *Prev;

// Data
G_LIST_TEMPLATE Item;

// Private methods
NODE(const NODE &); // Prevent usage
const NODE &operator=(const NODE &); // Prevent usage

public:
NODE(void);
NODE(NODE *NewNext, NODE *NewPrev, G_LIST_TEMPLATE NewItem);
~NODE(void);
};

// Data
NODE Head;
NODE Tail;

// Status
unsigned int Count;
unsigned int MaxSize;

// Private methods
void Remove(void); // Removes first element

G_LIST(const G_LIST &); // Prevent usage
const G_LIST &operator=(const G_LIST &); // Prevent usage

public:
G_LIST(const unsigned int Size);
~G_LIST(void);

// Public methods
void Insert(G_LIST_TEMPLATE InItem);
void Clear(void);

const vector<G_LIST_TEMPLATE> *GetEnd(const unsigned int Length);
const vector<G_LIST_TEMPLATE> *GetBeginning(const unsigned int Length);
};

//////////////////////
// Template methods //
//////////////////////

//////////////////
// Node Methods //
//////////////////

template <class G_LIST_TEMPLATE>
// Node constructor
G_LIST<G_LIST_TEMPLATE>::NODE::NODE(void)
{
Next = NULL;
Prev = NULL;
Item = NULL;
}

template <class G_LIST_TEMPLATE>
// Node constructor
G_LIST<G_LIST_TEMPLATE>::NODE::NODE(NODE *NewNext, NODE *NewPrev, G_LIST_TEMPLATE NewItem)
{
Next = NewNext;
Prev = NewPrev;
Item = NewItem;
}

template <class G_LIST_TEMPLATE>
// Node destructor
G_LIST<G_LIST_TEMPLATE>::NODE::~NODE(void)
{
Next = NULL;
Prev = NULL;

delete Item;
}

//////////////////
// List Methods //
//////////////////

template <class G_LIST_TEMPLATE>
G_LIST<G_LIST_TEMPLATE>::G_LIST(const unsigned int Size)
{
// Initialize status
MaxCount = Size;
Count = 0;

// Initialize head and tail nodes
Head.Next = &Tail;
Head.Prev = NULL;
Tail.Prev = &Head;
Tail.Next = NULL;
}

template <class G_LIST_TEMPLATE>
// List Destructor
G_LIST<G_LIST_TEMPLATE>::~G_LIST()
{
Clear(); // Empty the list
}

template <class G_LIST_TEMPLATE>
// Remove method
void G_LIST<G_LIST_TEMPLATE>::Remove(void)
{
NODE *Temp = Head.Next;
Head.Next = Temp->Next;
Head.Next->Prev = &Head;

delete Temp;

--Count;
}

template <class G_LIST_TEMPLATE>
// Insert method
void G_LIST<G_LIST_TEMPLATE>::Insert(G_LIST_TEMPLATE InItem)
{
if(Count < MaxCount)
{
NODE *NewNode = new NODE(&Tail, Tail.Prev, InItem);

Tail.Prev->Next = NewNode;
NewNode = NULL;
}
else
{
// Remove the first node from the list
NODE *TempNode = Head.Next;
Head.Next = TempNode->Next;
Head.Next->Prev = &Head;

// Re-use that node at the end
TempNode->Next = &Tail;
TempNode->Prev = &Tail.Prev;
TempNode->Prev->Next = TempNode;

delete TempNode->Item;

TempNode->Item = InItem;
}

++Count;
}

template <class G_LIST_TEMPLATE>
// Clear method
void G_LIST<G_LIST_TEMPLATE>::Clear(void)
{
while(Head->Next) // As long as there are elements in the list, remove
{
Remove();
}
}

#endif


Inherit list
g_event_list.h

/*
::::::::::::::::
:: g_events.h ::
::::::::::::::::

Contains the structure definition for generic engine wide messages

Created: June 11, 2005
Last Updated: June 12, 2005
*/

#ifndef G_EVENT_LIST_H
#define G_EVENT_LIST_H

#include "g_list.h" // Need templated list
#include "g_events.h" // Need event structure and event types

class G_EVENT_LIST : public G_LIST<G_EVENT *>
{
protected:
// Private classes
class NODE
{
protected:
NODE *Next;
NODE *Prev;

// Data
G_EVENT Item;

// Private methods
NODE(const NODE &); // Prevent usage
const NODE &operator=(const NODE &); // Prevent usage

public:
// Public methods
NODE(void);
NODE(NODE *NewNext, NODE *NewPrev, G_EVENT NewItem);
~NODE(void);
};

// Private methods
G_EVENT_LIST(const G_EVENT_LIST &); // Prevent usage
const G_EVENT_LIST &operator=(const G_EVENT_LIST &); // Prevent usage

public:
G_EVENT_LIST(void);
~G_EVENT_LIST(void);

// Public methods
const vector<G_EVENT *> *GetEnd(const unsigned int Length, const G_EVENT_TYPE Type);
const vector<G_EVENT *> *GetBeginning(const unsigned int Length, const G_EVENT_TYPE Type);
};

#endif


Inherit list
g_event_list.cpp

/*
::::::::::::::::
:: g_events.h ::
::::::::::::::::

Contains the structure definition for generic engine wide messages

Created: June 12, 2005
Last Updated: June 12, 2005
*/

#include "g_event_list.h"

// GetEnd method
const vector<G_EVENT *> *G_EVENT_LIST::GetEnd(const unsigned int Length, const G_EVENT_TYPE Type)
{
vector<G_EVENT *> *ReturnList = NULL;
NODE *CurrNode = Tail.Prev;
unsigned int CurrentLength = 0;

if(Count > 0) // Proceed only if there are elements
{
ReturnList = new vector<G_EVENT *>; // Create new vector only if needed

while(CurrentLength < Length && CurrNode != &Head) // While not at the end of the list, keep looking
{
if(Type == CurrNode->Item.Type || Type == GET_ALL) // If types match or all, add to list to return
{
ReturnList->push_back(&CurrNode->Item);
++CurrentLength;
CurrNode = CurrNode->Prev;
}
}
}

return ReturnList;
}

// GetBeginning method
const vector<G_EVENT *> *G_EVENT_LIST::GetBeginning(const unsigned int Length, const G_EVENT_TYPE Type)
{
vector<G_EVENT *> *ReturnList;
NODE *CurrNode = Head.Next;
unsigned int CurrentLength = 0;

if(Count > 0) // Proceed only if there are elements
{
ReturnList = new vector<G_EVENT *>; // Create new vector only if needed

while(CurrentLength < Length && CurrNode != &Tail) // While not at the end of the list, keep looking
{
if(Type == CurrNode->Item.Type || Type == GET_ALL) // If types match or all, add to list to return
{
ReturnList->push_back(&CurrNode->Item);
++CurrentLength;
CurrNode = CurrNode->Next;
}
}
}

return ReturnList;
}

Share this post


Link to post
Share on other sites
this should work, can't test atm...

template < typename T >
class List {
protected:
struct Node { };
};

struct Derived : List < int > {
void foo () { typename List< int >::Node node; }
};

Share this post


Link to post
Share on other sites
Quote:
Original post by Daishi
//...
virtual class NODE
//...


Well that is not valid, `virtual' can only be specified for member functions in standard C++, also it looks like your trying do some kind of redefining/overriding of nested class definitions [lol], seriously though that is impossible.

There is alot of odd things going on with your code, the oddest thing being that your using std::vector but writing your own custom list when the standard library already provides one.

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Quote:
Original post by Daishi
//...
virtual class NODE
//...


Well that is not valid, `virtual' can only be specified for member functions in standard C++, also it looks like your trying do some kind of redefining/overriding of nested class definitions [lol], seriously though that is impossible.

There is alot of odd things going on with your code, the oddest thing being that your using std::vector but writing your own custom list when the standard library already provides one.


That should not be there... I was just toying with it in some hopes that maybe it would work, since I had never seen/read anything about it before. But I forgot to change it back before posting.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!