namespace内部类的使用

假设有如下类

#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)
{
}

一般第二种更推荐。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值