【发布时间】:2014-08-26 12:35:00
【问题描述】:
我对 C++ 重载决议中的 A 行为感到困惑。我有2个班级,A和B,A
但是,当我使用引用调用虚函数时,它们似乎不像虚函数那样工作。起初我以为这是由于对象被分配在堆栈而不是堆上,但现在我发现即使我使用对在堆上分配的对象的引用也会发生这种奇怪的行为。
对此有何解释?
#include<iostream>
using namespace std;
class A{
public:
virtual void foo(){ cout << "A::foo" << endl; }
};
class B : public A{
public:
virtual void foo(){ cout << "B::foo" << endl; }
};
void test_pt(A* pt){
cout << "test_pt " << (int)pt << " ";
pt->foo();
}
void test_ref(A ref){
cout << "test_ref " << (int)&ref << " ";
ref.foo();
}
int main(int argc, char* argv[]){
// pointers to objects allocated on heap
A* heap_pt_a = new A;
B* heap_pt_b = new B;
// virtual functions work as intended
test_pt(heap_pt_a); // test_pt 4975912 A::foo
test_pt(heap_pt_b); // test_pt 4975960 B::foo
// references to objects allocated on heap
A heap_ref_a = *heap_pt_a;
B heap_ref_b = *heap_pt_b;
// virtual functions work as non-virtual
test_ref(stack_ref_a); // test_ref 1571400 A::foo
test_ref(stack_ref_b); // test_ref 1571400 A::foo
// references to objects allocated on stack
A stack_ref_a;
B stack_ref_b;
// virtual functions work as non-virtual
test_ref(stack_ref_a); // test_ref 1571400 A::foo
test_ref(stack_ref_b); // test_ref 1571400 A::foo
// references to stack used as pointers to stack
// virtual functions work as intended
test_pt(&stack_ref_a); // test_pt 1571724 A::foo
test_pt(&stack_ref_b); // test_pt 1571712 B::foo
return 0;
}
【问题讨论】:
标签: c++ reference overloading virtual-functions overload-resolution