【发布时间】:2013-02-28 18:02:45
【问题描述】:
我一直很难找到类似这个问题的东西,所以我会在这里问。
我有一个包含十几个源/头文件的项目。我遇到的主要问题是预定义我在命名空间中创建的类。代码如下:
“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*/
};
}
如果这听起来有点令人困惑,我提前道歉......
基本上我想知道是否可以预定义namespace myNamespace 中的类,同时声明shared_ptr。如果这是可能的,我将如何做到这一点并在源代码中正确使用它们?
【问题讨论】:
-
不要预定义任何东西,它只会让你的代码不那么可读。通过使用命名空间说明符,每个人都可以准确地知道您的类型是什么,而无需费力地阅读其他代码。 (但这只是我的看法)
标签: c++ class namespaces shared-ptr