C++程式訪問類的私有成員


類的私有成員只能被類本身的成員訪問。這樣做是為了保持面向物件正規化中的封裝性,確保資料及其相關函式保持在一個單元中,並且只能從該類的成員進行訪問。C++ 有三種不同的訪問說明符來指定類成員的可見性。這三種訪問說明符分別是:

  • 公共 (Public) - 如果類成員的可見性為公共,則可以從任何其他類訪問這些成員。

  • 私有 (Private) - 具有私有可見性的類成員只能從類內部訪問。

  • 保護 (Protected) - 保護類成員只能從類內部或其子類訪問。

本文將重點介紹如何僅訪問類的私有成員。

使用 getter 和 setter 方法訪問資料成員

getter 和 setter 函式用於訪問和修改類的私有成員。顧名思義,getter 函式返回資料成員,而 setter 函式用於“設定”或修改資料成員。我們透過兩個示例來進一步理解這個概念,但在此之前,先給出基本的語法。

語法

Getter/訪問器函式 -

private:
   <datatype> value;
public:
   <datatype> getterFunction() {
     return <datatype> this->value;
   }

Setter/修改器函式 -

private:
   <datatype> value;
public:
   void setterFunction(<datatype> _value) {
     this->value = _value;
   }

示例

#include <iostream>

using namespace std;

class Test{
   private:
      int value;
   public:
      //the getter function
      int getValue() {
         return this->value;
      }
      //the setter function
      void setValue(int _value) {
         this->value = _value;
      }
};

int main(){
   Test test;
   test.setValue(15);
   cout << "The value we set is: " << test.getValue() << endl;
   return 0;
}

輸出

The value we set is: 15

從另一個函式內部訪問成員函式

當我們訪問私有成員函式時,也是同樣的情況。我們必須從類成員方法內部訪問它,就像我們對資料成員所做的那樣。我們可以使用“this”指標來避免名稱衝突。

語法

private:
   <returntype> function_name(params) {};
public:
   <returntype> another_function(params) {
     <datatype> var = this->function_name(arguments);
   }

呼叫私有成員函式的函式應宣告為公共。只有當從該類的物件呼叫公共函式時,該函式才會執行。

示例

#include <iostream>

using namespace std;

class Test{
   private:
      int value;
      //multiplies the member value by 10
      void multiplyValue() {
         this->value = this->value * 10;
      }
   public:
      //the getvalue function calls the multiply value function
      int multiplyAndGetValue() {
         this->multiplyValue();
         return this->value;
      }
      //the setter function
      void setValue(int _value) {
         this->value = _value;
      }
};

int main(){
   Test test;
   test.setValue(15);
   cout << "The value after setting and multiplying is: " << test.multiplyAndGetValue() << endl;
   return 0;
}

輸出

The value after setting and multiplying is: 150

使用友元類

在 C++ 中,友元類是可以訪問另一個類私有和保護成員的類,而這些成員對於其他任何類都是不可見的。要將一個類宣告為另一個類的友元,需要使用關鍵字“friend”。讓我們看看它是如何工作的。

語法

class A{
   private:
     .....
   friend class B;
};

class B{
   //class body
};

示例

#include <iostream>

using namespace std;

class Test1{
   private:
      int value;
   public:
      Test1(int _value) {
         this->value = _value;
      }
      //we declare another class as a friend
      friend class Test2;
};

class Test2{
  public:
   //displays the value of the other class object
   void display(Test1 &t) {
      cout << "The value of Test1 object is: " << t.value;
   }
};

int main(){
   //creating two class objects of the two classes
   Test1 test1(15);
   Test2 test2;

   //calling the friend class function
   test2.display(test1);
   return 0;
}

輸出

The value of Test1 object is: 15

使用友元函式

C++ 中的友元函式類似於友元類。在這裡,我們可以將一個不屬於類的特定函式宣告為“友元”,它將獲得訪問該類私有成員的許可權。讓我們看看如何將函式定義為“friend”的語法。

語法

class A{
   private:
     .....
   friend <return_type> function_name(params);
};

<return_type> function_name(params) {
   //function body
}

示例

#include <iostream>

using namespace std;

class Test1{
   private:
      int value;
   public:
      Test1(int _value) {
         this->value = _value;
      }
      //we declare a friend function
      friend void display(Test1);
};

void display(Test1 t) {
   cout << "The value of Test1 object is: " << t.value;
}

int main(){
   //creating two class objects of the two classes
   Test1 test1(55);
   //calling the friend class function
   display(test1);
   return 0;
}

輸出

The value of Test1 object is: 55

結論

當我們訪問類的私有資料成員時,我們最好使用訪問器/getter 和修改器/setter 函式。這是訪問類資料成員最安全的方式。始終需要注意的是,訪問私有成員的函式應宣告為公共。其他面嚮物件語言中沒有友元函式,因為這並不能始終保持面向物件的封裝特性。友元不是相互的,如果類 A 將類 B 宣告為友元,則類 B 將可以訪問 A 的所有成員,但 A 無法訪問 B 的所有私有成員。

更新於:2022-12-13

23K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告