我想在一個類方法中擁有來自 C 庫 cmath 的方法,但沒有用我自己的方法覆寫它。我知道我可以更改名稱,但這不是我想要做的。這可能嗎 ?
計算器.cpp:
#include <calculator.h>
#include <cmath>
int Calculator::pow(int entier, int puissance) {
return pow(entier, puissance);
}
計算器.h:
class Calculator {
public:
Calculator() {}
int pow (int a, int b);
};
我已經知道我使用的型別對于這種型別的計算是錯誤的,但這不是重點。
uj5u.com熱心網友回復:
你沒有覆寫任何東西。您的功能與(或 global )pow在不同的范圍內。標準庫仍然存在,您的定義沒有改變。std::pow::powpow
只是非限定名稱查找只會找到在找到名稱宣告的最內層范圍內宣告的名稱的函式。
如果這不是你想要的,你需要限定然后 name 讓編譯器知道pow你想呼叫哪個,例如
return std::pow(entier, puissance);
呼叫pow標準庫命名空間中的函式std或
return ::pow(entier, puissance);
pow在全域命名空間范圍內呼叫函式。但是,包含<cmath>并不能保證標準庫pow函式將在全域命名空間范圍內宣告,這就是為什么無論如何您都應該使用std::pow(而不是::pow或只是)的原因。pow
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/508347.html
標籤:C
下一篇:在類定義與類實體化中分配類變數
