![](/img/trans.png)
[英]How do I put some code into multiple namespaces without duplicating this code?
[英]How do I put several functions into multiple namespaces without duplicating this code?
這是對以下內容的跟進:
我需要更改命名空間的名稱,但希望保持向后兼容性。 上面的解決方案表明我必須為每個函數執行以下操作:
namespace NewNamespaceName
{
void print()
{
//do work...
}
// 50 other functions
}
namespace OldNameSpaceName
{
using NewNamespaceName::print;
// 50 other using declarations
}
我的問題:有更簡單的方法嗎?
只需使用命名空間別名:
namespace OldNameSpaceName = NewNameSpaceName;
你可以干脆做
namespace NewNamespaceName
{
void print()
{
//do work...
}
// 50 other functions
}
namespace OldNameSpaceName
{
using namespace NewNamespaceName;
}
如果NewNamespaceName
包含您想要包含的其他內容,但不希望它們位於OldNamespaceName
,那么只需創建另一個私有名稱空間並將其導入舊名稱空間
namespace NewNamespaceName
{
namespace Private {
void print() { ... }
// 50 other functions
}
}
namespace OldNameSpaceName
{
using namespace NewNamespaceName::Private;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.