Ruslan F. Ruslan F. - 1 year ago 67
C++ Question

forward declaration of typedef class

For example we have some class with its ptr

typedef
ed

class Foo
{
....
};
typedef shared_ptr<Foo> FooPtr;


Is it possible to forward declare
FooPtr
? The purpose would be to avoid including the header with the
FooPtr
definition.

Update 1
Code I tried

another.h

class FooPtr;
calss A
{
.....
FooPtr m_pFoo;
.....
}


compiler error

Foo.h: error C2371: 'FooPtr' : redefinition; different basic types
1> another.h(7) : see declaration of 'FooPtr'

Answer Source

Yes, it should be totally possible, because shared_ptr is designed to be safe version of a pointer and pointers to incomplete types are allowed (just like references).

So yes, you can remove this dependency and replace body of Foo (or include of its header) with simple forward declaration:

class Foo;
typedef shared_ptr<Foo> FooPtr;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download