[英]Access typedef in classes with an object, not the scope resolution operator (::)
在下面的代碼中,當我嘗試使用實例化對象訪問typedef時,它給我一個錯誤,當我使用范圍解析運算符(::)訪問它時,該程序可以正常運行。 我只是想知道為什么。
#include <iostream>
class Types {
public:
typedef int Integer;
};
int main() {
Types types;
types.Integer foo = 1; // <-- Gives me an error
Types::Integer goo = 2; // <-- Works perfectly fine
std::cout << foo;
std::cout << std::endl;
std::cout << goo;
return 0;
}
我只是以這個為例,這並不是任何東西的真實代碼。 它給我的錯誤是:
Line 15 | invalid use of 'Types::Integer'
語法就是這樣工作的。 在這種情況下, Integer
是一種屬於Types
名稱空間的Types
,如果要訪問該類型,則必須使用::
。 operator.
用於對象或功能的成員訪問。
operator.
允許您訪問屬於實例的成員,而::
遍歷名稱空間(允許您訪問靜態字段,靜態函數,typedef,成員變量等)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.