假设有如下类
#ifndef TESTCLASS_H
#define TESTCLASS_H
namespace Firstspace {
namespace Secondspace {
class Testclass {
//内部定义
};
typedef QSharedPointer<Testclass> PTestclass; //常定义智能指针别名
}//Firstspace end
}//Secondspace end
#endif // TESTCLASS_H
使用方法1
//在使用的.h中includ
#include testclass.h
using namespace Firstspace::Secondspace;
使用方法2
//如果不想放在.h中使用,一般放.h中使用,容易导致相互include,编译error。可以在cpp中include
//这样的话就需要在.h中声明这个类。
//.h文件中声明
namespace Firstspace {
namespace Secondspace{
class TestClass;
}
}
比如.h文件中使用了TestClass,直接用就行,但是不能有类的操作,只能作为声明
void getTestName(Firstspace::Secondspace::TestClass xxx);
//在.cpp中,再include
#include testclass.h
using namespace Firstspace::Secondspace;
void getTestName(TestClass xxx)
{
}
一般第二种更推荐。