最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入解析C++編程中范圍解析運(yùn)算符的作用及使用

 更新時(shí)間:2016年01月14日 16:05:42   投稿:goldensun  
這篇文章主要介紹了C++編程中范圍解析運(yùn)算符的使用方法,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

范圍解析運(yùn)算符 :: 用于標(biāo)識(shí)和消除在不同范圍內(nèi)使用的標(biāo)識(shí)符。
語(yǔ)法

復(fù)制代碼 代碼如下:

:: identifier class-name :: identifier namespace :: identifier enum class :: identifier enum struct :: identifier


備注
identifier 可以是變量、函數(shù)或枚舉值。
具有命名空間和類
以下示例顯示范圍解析運(yùn)算符如何與命名空間和類一起使用:

namespace NamespaceA{
  int x;
  class ClassA {
  public:
    int x;
  };
}

int main() {

  // A namespace name used to disambiguate
  NamespaceA::x = 1;

  // A class name used to disambiguate
  NamespaceA::ClassA a1;
  a1.x = 2;

}

沒(méi)有范圍限定符的范圍解析運(yùn)算符表示全局命名空間。

namespace NamespaceA{
  int x;
}

int x; 

int main() {
  int x;

  // the x in main()
  x = 0; 
  // The x in the global namespace
  ::x = 1; 

  // The x in the A namespace
  NamespaceA::x = 2; 
}

你可以使用范圍解析運(yùn)算符來(lái)標(biāo)識(shí)命名空間的成員,還可標(biāo)識(shí)通過(guò) using 指定成員的命名空間的命名空間。在下面的示例中,你可以使用 NamespaceC 限定 ClassB(盡管 ClassB 已在 NamespaceB 中聲明),因?yàn)橐淹ㄟ^(guò) using 指令在 NamespaceC 中指定 NamespaceB。

namespace NamespaceB {
  class ClassB {
  public:
    int x;
  };
}

namespace NamespaceC{
  using namespace B;

}
int main() {
  NamespaceB::ClassB c_b;
  NamespaceC::ClassB c_c;

  c_b.x = 3;
  c_c.x = 4;
}

可使用范圍解析運(yùn)算符鏈。在以下示例中,NamespaceD::NamespaceD1 將標(biāo)識(shí)嵌套的命名空間 NamespaceD1,并且 NamespaceE::ClassE::ClassE1 將標(biāo)識(shí)嵌套的類 ClassE1。

namespace NamespaceD{
  namespace NamespaceD1{
    int x;
  }
}

namespace NamespaceE{

  class ClassE{
  public:
    class ClassE1{
    public:
      int x;
    };
  };
}

int main() {
  NamespaceD:: NamespaceD1::x = 6;
  NamespaceE::ClassE::ClassE1 e1;
  e1.x = 7 ;
}

具有靜態(tài)成員
必須使用范圍解析運(yùn)算符來(lái)調(diào)用類的靜態(tài)成員。

class ClassG {
public:
  static int get_x() { return x;}
  static int x;
};

int ClassG::x = 6;

int main() {

  int gx1 = ClassG::x;
  int gx2 = ClassG::get_x(); 
}

具有區(qū)分范圍的枚舉
區(qū)分范圍的解析運(yùn)算符還可以與區(qū)分范圍的枚舉枚舉聲明的值一起使用,如下例所示:

enum class EnumA{
  First,
  Second,
  Third
};

int main() {

  EnumA enum_value = EnumA::First;
}

相關(guān)文章

最新評(píng)論

靖江市| 盈江县| 申扎县| 体育| 微山县| 元氏县| 明溪县| 交城县| 中卫市| 册亨县| 当涂县| 东海县| 双流县| 中西区| 仁寿县| 寻乌县| 晋中市| 常山县| 虞城县| 名山县| 大连市| 襄樊市| 晴隆县| 和田县| 兴宁市| 武冈市| 仙居县| 灯塔市| 甘孜| 寿阳县| 凤冈县| 根河市| 临猗县| 巴南区| 康平县| 武鸣县| 新丰县| 尚志市| 滦平县| 罗源县| 尼玛县|