Compiling C++ when two classes references one another
- by Omry
I am trying to write a simple wrapper around a connection pointer that will return it to the pool when the wrapper is destroyed. but it wont compile because the ConnectionPool and AutoConn need each other to be declared.
I tried to use forward deceleration but it didn't work. how do I solve this? (using g++)
class Connection {};
class ConnectionPool
{
Connection *m_c;
public:
AutoConn getConn()
{
return AutoConn(this, m_c); // by value
}
void releaseConnection(Connection *c)
{
}
};
class AutoConn
{
ConnectionPool* m_pool;
Connection *m_connection;
public:
AutoConn(ConnectionPool* pool, Connection *c) : m_pool(pool), m_connection(c) {}
~AutoConn()
{
m_pool->releaseConnection(m_connection);
}
};