I have been having a hard time trying to find anything similar to this question, so instead I will ask here.
I have a project with a dozen or so source/header files. The main problem I am having is predefining the classes that I have made in the namespace. The code is as followed:
"GlobalIncludes.h"
/*include dependencies and library headers...*/
/*[Note 1]How would I predefine the classes inside namespaces?*/
typedef std::tr1::shared_ptr<Class1> ClassPtr1;//[Note 2]
typedef std::tr1::shared_ptr<Class2> ClassPtr2;//[Note 2]
/*[Note 2]What is the correct way to predefine the shared_ptr's?*/
#include "Class1.h"
#include "Class2.h"
"Class1.h"
namespace myNamespace
{
class Class1
{
/*variables and functions*/
void doSomething(...);
Class2 exampleObject;
};
}
"Class2.h"
namespace myNamespace
{
class Class2
{
/*variables and functions*/
};
}
My apologies in advance if this sounds a bit confusing...
Basically I am wondering if it is possible to predefine the classes that are in namespace myNamespace
and at the same time declare the shared_ptr
's. If this is possible, how would I do this and use them correctly in the source?
If you want the type definitions to be part of the same namespace as the classes (which I suggest):
namespace my_namespace
{
class Class1;
class Class2;
typedef std::tr1::shared_ptr<Class1> ClassPtr1;
typedef std::tr1::shared_ptr<Class2> ClassPtr2;
}
#include "Class1.h"
#include "Class2.h"
Otherwise, if you want your pointer type definitions to be part of the global namespace
namespace my_namespace
{
class Class1;
class Class2;
}
typedef std::tr1::shared_ptr<my_namespace::Class1> ClassPtr1;
typedef std::tr1::shared_ptr<my_namespace::Class2> ClassPtr2;
#include "Class1.h"
#include "Class2.h"
Possibly, you could make things more compact with a macro (same namespace):
#define DECLARE_PTR_ALIAS(N, C, P) \
namespace N { class C;
typedef std::tr1::shared_ptr<C> P; } \
Or (different namespace):
#define DECLARE_PTR_ALIAS(N, C, P) \
namespace N { class C; } \
typedef std::tr1::shared_ptr<N::C> P;
This would make it simpler to define pointer aliases for several classes:
DECLARE_PTR_ALIAS(my_namespace, Class1, ClassPtr1)
DECLARE_PTR_ALIAS(my_namespace, Class2, ClassPtr2)
...