Jump to content
  • Advertisement
Sign in to follow this  
Spinewire

Std::list and inherited types?

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

Hello everyone, I'm trying to implement a 'Manager' and 'Node' system, and I was wondering if there was some variant of this code that would do that I'm trying to achieve. Node.h
class Node
{
public:
   virtual void Update( void );
   virtual void Release( void );
};
typedef Node *LPNODE;


class ExtendedNodeA : public Node
{
protected:
   // Some pointers here
   // ...
   // ...

public:
   // Constructor/Destructor...
   ///
   void Release( void ); // Frees all the allocated memory above
   void Update( UINT someNumber );
};
typedef ExtendedNodeA *LPEXTENDEDNODEA


class ExtendedNodeB : public Node
{
protected:
   // Some pointers here
   // ...
   // ...

public:
   // Constructor/Destructor...
   ///
   void Release( void ); // Frees all the allocated memory above
   void Update( CHAR someChar, FLOAT someFloat );
};
typedef ExtendedNodeB *LPEXTENDEDNODEB




Node.cpp omitted (not really important here) Manager.h
class Manager
{
protected:
   std::list<LPNODE> m_nodeList;

   void AddNode( LPNODE pNode ) { m_nodeList.push_front( pNode ); }
   void RemoveNode( LPNODE pNode ) { m_nodeList.remove( pNode ); }

public:
   void ReleaseAll( void );

};


class ExtendedManagerA : public Manager
{
public:
   // This is to prevent adding of any non-LPEXTENDEDNODEA pointers
   void AddNodeA( LPEXTENDEDNODEA pNodeA ) { AddNode( pNodeA ); }
   void RemoveNodeA( LPEXTENDEDNODEA pNodeA ) { RemoveNode( pNodeA ); }

   // The key difference here is in the parameters
   void UpdateAll( UINT someNumber );
};


class ExtendedManagerB : public Manager
{
public:
   // This is to prevent adding of any non-LPEXTENDEDNODEB pointers
   void AddNodeB( LPEXTENDEDNODEB pNodeB ) { AddNode( pNodeB ); }
   void RemoveNodeB( LPEXTENDEDNODEB pNodeB ) { RemoveNode( pNodeB ); }

   // The key difference here is in the parameters
   void UpdateAll( CHAR someChar, FLOAT someFloat );
};




Manager.cpp
void Manager::ReleaseAll( void )
{
   std::list<LPNODE>::iterator tempIterator;
   for ( tempIterator = m_nodeList.begin(); tempIterator != m_nodeList.end(); tempIterator++ )
   {
      ( *tempIterator )->Release()
   }
   m_nodeList.clear()
}


void ExtendedManagerA::UpdateAll( UINT someNumber )
{
   // The typing of the iterator here is illegal, I know
   std::list<LPEXTENDEDNODEA>::iterator tempIterator;
   for ( tempIterator = m_nodeList.begin(); tempIterator != m_nodeList.end(); tempIterator++ )
   {
      ( *tempIterator )->Update( someNumber );
   }
   m_nodeList.clear()
}


void ExtendedManagerB::UpdateAll( CHAR someChar, FLOAT someFloat )
{
   // The typing of the iterator here is illegal, I know
   std::list<LPEXTENDEDNODEB>::iterator tempIterator;
   for ( tempIterator = m_nodeList.begin(); tempIterator != m_nodeList.end(); tempIterator++ )
   {
      ( *tempIterator )->Update( someChar, someFloat );
   }
   m_nodeList.clear()
}




So, in summary, since the Release() function of all of my nodes has the same signature (void and void), I'd like the code to release and clear the list to be inherited from the parent 'Manager' class. However, since the update methods' signatures differs between the extended nodes, each extended manager should have its own 'update' method. Because the list's type is the base class, I'm unable to access the Update() method of the subclasses because of the differing signatures. Is there any way to get around this? I have a handful of classes that interact with this Node/Manager relationship, and I would like to eliminate this redundancy. I know I could crudely work around this by putting the signatures for each of the Update() methods I use in the base Node class, but that strikes me as very ugly. Is there a cleaner way?Thanks!

Share this post


Link to post
Share on other sites
Advertisement
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!