[英]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" “ 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" “ Class1.h”
namespace myNamespace
{
class Class1
{
/*variables and functions*/
void doSomething(...);
Class2 exampleObject;
};
}
"Class2.h" “ 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. 如果这听起来有点令人困惑,我会提前道歉...基本上,我想知道是否可以预定义
namespace myNamespace
的类,并同时声明shared_ptr
的类。 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: 但是,在您的示例中,Class1已经在名称空间中,因此您最好这样做:
namespace myNamespace
{
class Class2;
class Class1
{
/*variables and functions*/
void doSomething(...);
Class2 exampleObject;
};
}
The only problem is Class1 has Class2 as a member-variable. 唯一的问题是Class1具有Class2作为成员变量。 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: 如果您将Class2设为智能指针,则可以这样做:
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. 但是shared_ptr必须被完全包含,并且不能预先声明,因为shared_ptrs现在是成员变量,并且该类需要知道其完整大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.