C++中范围解析运算符(详细指南)

297 阅读3分钟

Scope Resolution :: in C++

在这篇文章中,我们探讨了**C++中范围解析运算符::**的概念,它在C++的实现中被广泛用于访问另一个类中的一个类,在多重继承中,还有更多。

目录:

  1. 范围解析的基础知识:: 在C++中
  2. 使用范围解析的C++实例的实现
  3. 在C++中使用Scope Resolution ::的不同方法
    • 在一个类之外定义一个函数
    • 访问一个类的静态变量
    • 在多重继承的情况下
    • 对于命名空间
    • 在另一个类中引用一个类
  4. 应用

让我们开始学习C++编程语言中的范围解析吧。

C++中的范围解析::的基础知识

范围解析操作符**'::'**是一个操作符,它有助于识别和指定一个标识符所指向的上下文,特别是通过指定一个命名空间,或者也可以说它是用来限定隐藏的名字,以便你仍然可以使用它们。如果一个名字空间范围或全局范围的名字被一个块或类中相同名字的明确声明所隐藏,你可以使用单项范围操作符。

例子--

如果你有一个名为Apple的全局变量和一个名为Apple的局部变量,要访问全局Apple,你需要使用范围解析操作符。

使用范围解析的C++例子的实现

#include<bits/stdc++.h>
using namespace std;
string name="OPENGENUS";    
// not decalred in any function known as global 
int main()
{
    string name; // declared inside a main function known as local 
    cin >> name;
    cout << "\nThe name you entered will be displayed " 
       << name << endl;

    // Using Scope Resolution :: in C++
    cout << "The name which you declared globally will be displayed " 
       << ::name << endl;
    return 0;
}

OUTPUT:

opengenus
The name you entered will be displayed opengenus
The name which you declared globally will be displayed OPENGENUS

在C++中使用范围解析 :: 的不同方法

在C++中,范围解析::可以被用来作为:

  1. 在一个类之外定义一个函数
  2. 访问一个类的静态变量
  3. 在多重继承的情况下
  4. 用于命名空间
  5. 指向另一个类中的一个类

它还可以用于以下目的。

1.在一个类外定义一个函数

当我们在一个类中声明一个函数时,它对该类的数据成员起作用。现在要在类的定义之外访问该声明的函数,以便进一步定义、实现或仅仅调用该函数,我们使用范围解析操作符。

范围解析操作符::是用来在类外定义一个函数的

#include<bits/stdc++.h> 
using namespace std;
class Dog
{
    public: 
    // Only declaration of the properties of Dog class 
   void colour();
   int size();
};
  
// Now here we are given defination to the decalred property colour in the class Dog 
void Dog :: colour()
{
   cout<<"brown"<<endl;
}
int Dog :: size()
{
    cout<<"3"<<endl;
  return 0;
}
  
int main()
{
   Dog goofy; // creating an object of the class Dog
  goofy.colour(); // here getting the colour of goofy
   goofy.size();// size of goofy
   return 0;
}

OUTPUT

brown
3

2.访问一个类的静态变量

让我们考虑这样一种情况:你有一个名为 "x=3 "的静态变量和一个名为 "x=4 "的局部变量,我们知道静态变量的值永远不会改变,但是在一个有局部变量 "x=4 "的函数中,我们想访问值为 "x=3 "的静态变量,我们将使用操作符,否则它将给我们一个局部变量的值,即 "x=4"。

#include<bits/stdc++.h>
using namespace std;
class Dog
{
	static string name;
public:
	static int size;

	// what happens here is loacl parameter name value will be displayed 
  // everytime inside this function despite of the fact that a static variable of it already exsits
  // so in this case we can acces the static variable using "::"
	void func(string name)
	{
	// We can access class's static variable
	// even if there is a local variable
	cout << "Value of static name is " << Dog::name<<endl;

	cout << "Value of local name is " << name<<endl;
	}
};
// In C++, static members must be explicitly defined
// like this
string Dog::name = "scooby";
int Dog :: size = 4;

int main()
{
	Dog d; // creating object of the class
	string name;
  cin>>name;
	d.func(name);
	cout <<"Dispalying the size of static variable size = " << Dog::size;
	return 0;
}

OUTPUT

goofy
Value of static name is scooby
Value of local name is goofy
Dispalying the size of static variable size = 4

3.在多重继承的情况下

假设我们有一个父类,它有两个子类(两个不同的子类),现在我们想知道这两个子类的性别和年龄,所以我们需要两个函数来显示这两个子类中的年龄和性别,所以为了唯一地访问这两个子类的年龄和性别,我们将使用范围解析操作符。

// Use of scope resolution operator in multiple inheritance.
#include<bits/stdc++.h>
using namespace std;
class Anime
{
  
  public:
	string name; 
  int episodes;
  void getdata(string x, int y)        //SAME NAME
  {
    name = x;
    episodes = y;
  }
  void display()
  {
    cout << "Anime is" << name <<" with "<<episodes<<"episodes"<< endl;
  }
};
class Character
{
public:
  string name;
  void getdata(string q)                  // SAME NAME
  {
    name = q;
  }
  void display()
  {
    cout << "Name of Favorite character" <<name<< endl;
  }
};
// multiple classes getting inherited by details class
class details: public Anime, public Character
{
public:
void display()
  {
    Anime::getdata("AOT", 70); // used to getdata of anime class
    Anime::display();
    Character::getdata("LEVI"); // used to getdata of character class
    Character::display();
  }
};

int main()
{
  details d;
  d.display();
  return 0;
}

OUTPUT

Anime is AOT with 70 episodes
Name of Favorite character LEVI

4.对于名字空间

如果一个具有相同名字的类存在于两个命名空间中,我们可以使用命名空间的名字和范围解析操作符来引用该类而不发生任何冲突。

// Use of scope resolution operator for namespace.
#include<bits/stdc++.h>
int main(){
// all these std :: was taken care by namespace but not defined so to avoid conflicts.
	std::cout << "Hello World" << std::endl;
}

5.指向另一个类中的一个类

让我们考虑一下我们的食物(父类),在我们的父类里面有不同的类(即一个类在另一个类里面),所以在这种情况下,要访问一个存在于另一个类里面的类,我们将使用范围解析操作符。

#include<bits/stdc++.h>
using namespace std;
class Colours // main class 
{
public:
	static string x;

	class Light      // another class inside main class
	{
	public:
			string x;
      static string y;
	};

  class Dark        // another cass inside main class
  {
    public:
     string x;
     static string y;
  };
};
string Colours ::x="Blue";
string Colours::Light::y = "Sky blue"; // value of class light
string Colours::Dark:: y="Dark blue"; // value of class dark
int main()
{
  Colours A;
  Colours::Light B;
  Colours::Dark C;
  cout<<A.x<<endl; // displaying values of main class
  cout<< B.y<<endl;  // displaying value of light class inside main class
  cout<<C.y<<endl; // dispalying value of Dark class inside main class
  return 0;
}

OUTPUT

Blue
Sky blue
Dark blue

应用

  • 在一个具有相同名称的局部变量的函数中访问一个全局变量的值。
  • 它也被用来在类的定义之外定义和调用一个函数。
  • 对于库的程序员来说,在其各自的类之外定义成员函数是可能的,通常也是必要的。

通过OpenGenus的这篇文章,你一定对C++中使用Scope Resolution :: 操作符有了完整的认识。