简体   繁体   中英

What is the correct way to “predefine” and use namespaces and std::shared_ptr?

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)
...

To pre-declare classes (or functions, or etc...) in a namespace, you do this:

namespace myNamespace
{
    class myClassA;
    class myClassB;
}

If I'm just pre-declaring a single class, I like to put it on one line: (just personal preference)

namespace myNamespace { class myClassA; }

In your example, however, Class1 is already in a namespace, so you might as well do it like this:

namespace myNamespace
{
    class Class2;

    class Class1
    {
        /*variables and functions*/
        void doSomething(...);
        Class2 exampleObject;
    };
}

The only problem is Class1 has Class2 as a member-variable. You can't use pre-declared objects as members, since the compiler needs to know the size of the object to embed it into the class. You can only use pre-declared objects as references or pointers, since those have fixed sizes.

If you made Class2 a smart pointer, then you can do so:

namespace myNamespace
{
    class Class2;

    typedef std::shared_ptr<Class2> Class2ptr;

    class Class1
    {
        /*variables and functions*/
        void doSomething(...);
        Class2ptr exampleObject;
    };
}

But the shared_ptr has to be fully included and can't be pre-declared, because the shared_ptrs are now member variables and the class needs to know their full size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM