views:

219

answers:

7

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);
    }
};
+4  A: 

Use forward declaration:

class Connection {};

class ConnectionPool; //<<<<<<<<<<<<<<<forward declaration

class AutoConn {
//definitions
};

class ConnectionPool {
//definitions
};
sharptooth
I suggest you try to compile it. it does not compile.
Omry
+2  A: 

implement the functions after the point where the classes are defined

stefaanv
A: 

You might want to outsource the definition of all ConnectionPool and AutoConn methods, i.e.

class ConnectionPool;
class AutoConn {…};

class ConnectionPool {…};

AutoConn ConnectionPool::getConn() {
   …
}
Alexander Gessler
+3  A: 

The correct syntax for a forward declaration is:

class Connection; // no {}

If you write

class Connection {};

Then you are defining the class, and you can't define a class twice.

Also, shouldn't you be forward declaring AutoConn, not Connection?

Peter Alexander
this is not forward deceleration, it's just a deceleration so the sample compiled.
Omry
He did not define it twice; that is the *only* definition of Connection. I am assuming it is just a place-holder to reduce the amount of posted code.
Clifford
Oh, I thought that was your attempt at the forward declaration - my apologies.
Peter Alexander
A: 

Don't include ConnectionPool header file in AutoConn. Just use a forward reference like class ConnectionPool; in the AutoConn header file.

Naveen
+1  A: 

Forward declaration only tells the compiler "such a class exists". In your

AutoConn getConn()

since AutoConn is a value type, the whole structure of AutoConn must be known, so forward declaration of the class won't work. So you must put the actual declaration of AutoConn before ConnectionPool.

In your AutoConn, the type ConnectionPool is only referred by pointers. In this case the whole structure of ConnectionPool is not required, so forward declaration of ConnectionPool is enough.

Therefore you need to rearrangement the classes into this:

class Connection;
class ConnectionPool;
class AutoConn { ... };
class ConnectionPool { ... };

But notice that

AutoConn(ConnectionPool* pool, Connection *c) : m_pool(pool), m_connection(c) {}
~AutoConn()
{
    m_pool->releaseConnection(m_connection);
}

these methods require the compiler to know the members of ConnectionPool, so a complete structure is needed. To solve this problem the definition must be placed after ConnectionPool. Thus only the constructors and destructors should remain.

class AutoConn {
  ...
  AutoConn(ConnectionPool* pool, Connection *c);
  ~AutoConn();
}
class ConnectionPool { ... };
AutoConn::AutoConn(ConnectionPool* pool, Connection *c) : ... { ... }
AutoConn::~AutoConn() { ... }
KennyTM
this is also a correct answer. +1 for the good answer.
Omry
+6  A: 

A combination of forward declaration and separation of declaration from definition of members with circular dependencies works. For example:

class Connection {};
class ConnectionPool ;

class AutoConn
{

    ConnectionPool* m_pool;
    Connection *m_connection;
public:
    AutoConn(ConnectionPool* pool, Connection *c) : m_pool(pool), m_connection(c) {}
    ~AutoConn() ;  // Not defined here because it accesses unknown members of class Connection
} ;

class ConnectionPool
{
    Connection *m_c;
public: 
    AutoConn getConn()
    {
        return AutoConn(this, m_c); // by value
    }

    void releaseConnection(Connection *c)
    {
    }
};

// Definition of destructor with class Connection member dependencies.
AutoConn::~AutoConn()
{
    m_pool->releaseConnection(m_connection);
}
Clifford
great, finally a real answer :). thanks!
Omry