我试图理解 C++ 中“virtual”关键字的功能——考虑这个例子:
#ifdef USE_VIRTUAL
struct a {
virtual void say_hi() { std::cout << "hello from a" << std::endl; }
};
#else
struct a {
void say_hi() { std::cout << "hello from a" << std::endl; }
};
#endif
struct b : public a {
void say_hi() { std::cout << "hello from b" << std::endl; }
};
int main( int argc, char** argc )
{
a a_obj;
b b_obj;
a_obj.say_hi();
b_obj.say_hi();
}
这个程序输出:
hello from a
hello from b
不管 a::say_hi 是否声明为虚拟的。由于即使未将 say_hi 声明为虚拟,该函数也会被正确覆盖,那么将其声明为虚拟的功能是什么?
最佳答案
您没有使用多态性。多态行为只影响指针和对基类的引用,如下所示:
class A; class B1 : A; class B2 : A;
B1 x;
B2 y;
A z;
A & a1 = x; // static type A&, dynamic type B1&
A & a2 = y; // static type A&, dynamic type B2&
A & a3 = z; // static and dynamic type A&
现在访问 a1
、a2
、a3
的成员函数受制于多态和虚拟分派(dispatch)。
但是,您必须在继承层次结构的顶部声明第一个函数为虚函数,即在 A
中!在您的示例中,没有 virtual
,就没有多态性,您总是调用对象的相应 static 类型的成员函数。要对此进行测试,请添加另外几行:
a & bref = b_obj;
bref.say_hi(); // call b::say_hi() if virtual, a::say_hi if not
bref.a::say_hi(); // always call a::say_hi()
关于c++ - "virtual"关键字在 C++ 的基类中是可选的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7218763/