双冒号在C++中有三个用途
1:表示类成员
假设有A、B两类,他们都有一个成员变量member
那么使用双冒号加以区分。A::member,B::member
2:在类外定义类方法
Class Player{
public:
void Speak(const string& words);
}
void Player::Speak(const string& words){
cout<< "Player said: " << words <<endl;
}
3:表示全局变量
假设某函数或方法内有一个变量a与某全局变量a重名
那么全局变量写作::a与a区分
int a = 1;
void add(int a){
cout<< a + ::a << endl;;
};
int main(){
add(5);//Output:6
};