![](/img/trans.png)
[英]c++ How do you call a templated base-class function from derived class instance
[英]c++ How do you call a template class function that is overloaded in the derived class, from the derived class?
是否有一種方法可以在派生類中重載基模板類函數,並且仍然能夠調用基類函數,如下所述?
template <class A>
class testBase {
public:
void insert() {
// Whatever
}
};
class testDerived : public testBase<double> {
private:
void insert(int a) {
// something
}
};
int main() {
testDerived B;
// Compiler doesn't recognize overloaded insert,
// How do you do this?
B.insert();
}
您遇到的問題稱為“ 隱藏名稱” 。
您可以通過按名稱限定基類功能來調用它:
B.testBase<double>::insert();
或通過“取消隱藏”它,通過使用using聲明在派生類中聲明它:
class testDerived : public testBase<double> {
public:
using testBase<double>::insert;
private:
void insert(int a) {
// something
}
};
加
public:
using testBase<double>::insert;
到testDerived
的定義。 這將使基礎的隱藏成員在派生類中可用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.