在命名空間內使變數和函式行內和靜態有什么意義。例如考慮以下 2 個檔案:
考慮第一個頭檔案
// header1.h
#include <string>
#include <iostream>
namespace First
{
static inline std::string s {"hi"};
static inline void print(std::string str)
{
std::cout << str << std::end;
}
}
并考慮遵循第二個頭檔案
// header2.h
#include <string>
#include <iostream>
namespace Second
{
std::string s {"hi"};
void print(std::string str)
{
std::cout << str << std::end;
}
}
編譯器對兩個代碼的看法不同還是它們引入了相似的行為?
uj5u.com熱心網友回復:
命名空間變數和函式是否沒有內部鏈接,例如未命名的命名空間?
不,您認為默認情況下命名空間(命名空間)變數和函式具有內部鏈接的假設是不正確的。
編譯器對兩個代碼的看法不同還是它們引入了相似的行為?
帶有static關鍵字的一個具有內部鏈接,而另一個(在 header2.h 中)具有外部鏈接。
也就是說,sheader1.h 和 header2.h 中的變數彼此不同。同樣,功能也各不相同。
header1.h
namespace First
{
static inline std::string s {"hi"}; //s has internal linkage
static inline void print(std::string str) //print has internal linkage
{
std::cout << str << std::end;
}
}
header2.h
namespace Second
{
std::string s {"hi"}; //s has external linkage
void print(std::string str) //print has external linkage
{
std::cout << str << std::end;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/493994.html
標籤:C
上一篇:有人可以解釋冒號在這種情況下的作用嗎?以及此代碼的總體定義/
下一篇:如何知道何時創建副本?
