在 C++ 中無法過載的函式


在 C++ 中,我們可以過載函式。但有時不會進行過載。在本節中,我們將看到在哪些不同情況下,我們無法過載函式。

  • 當函式簽名相同,只有返回值型別不同,那麼我們無法過載函式。

int my_func() {
   return 5;
}
char my_func() {
   return 'd';
}
  • 當成員函式在類中具有相同的名稱和相同引數列表時,它們無法過載。

class My_Class{
   static void func(int x) {
      //Something
   }
   void func(int x) {
      //something
   }
};
  • 當引數宣告僅在指標 * 和陣列 [] 中不同時,它們相同。

int my_func(int *arr) {
   //Do something
}
int my_func(int arr[]) {
   //do something
}
  • 當引數宣告僅在常數或 volatile 限定符的存在中不同時,它們相同。

int my_func(int x) {
   //Do something
}
int my_func(const int x) {
   //do something
}
  • 當引數宣告僅在它們的預設引數中不同且相等時。

int my_func(int a, int b) {
   //Do something
}
int my_func(int a, int b = 50) {
   //do something
}

更新於:2019-07-30

299 次瀏覽

開啟你的 事業

完成課程獲得認證

開始學習
廣告
© . All rights reserved.