0
爲什麼d.f(1)
在這段代碼中調用Derived::f
?C++ using-declarations從基類調用函數
using Base::f
在決定哪個f
將被調用時起作用嗎?
#include <iostream>
using namespace std;
struct Base {
void f(int){
cout << "\n f(Base) is called" ;
}
};
struct Derived : Base {
using Base::f ; // using-declarations but still Drived function is called
void f(int){
cout << "\n f(Derived) is called" ;
}
};
void use(Derived d)
{
d.f(1); // calls Derived::f
Base& br = d ;
br.f(1); // calls Base::f
}
int main() {
Derived d;
use (d);
return 0;
}
'僅使用Base :: f'只適用於在Derived中沒有超級函數的情況。只要你有一個聲明/定義,這個被稱爲。 – 2015-02-05 18:38:49
如果Base :: f和Derived :: f有不同的參數(這裏隱藏了唯一(使用)函數和派生函數) – 2015-02-05 18:41:17
當我在VS2013 Update 4中運行代碼時,它調用派生,然後基地。你是否在說它是派生的,然後派生出來的? – 2015-02-05 18:57:29