C++虚函数的概念
concept of virtual functions in c++
我想了解 C++ 中虚函数的概念,我在网上阅读了它,但我无法理解为什么下面的程序输出是 2 而不是 1?谁能解释一下?
Class A
{
int a;
public:
A()
{
a = 1;
}
virtual void show()
{
cout <<a;
}
};
Class B: public A
{
int b;
public:
B()
{
b = 2;
}
virtual void show()
{
cout <<b;
}
};
int main()
{
A *pA;
B oB;
pA = &oB;
pA->show();
return 0;
}
Virtual functions are member functions whose behavior can be overridden in derived classes. As opposed to non-virtual functions, the overridden behavior is preserved even if there is no compile-time information about the actual type of the class. If a derived class is handled using pointer or reference to the base class, a call to an overridden virtual function would invoke the behavior defined in the derived class. This behavior is suppressed if the function is selected using qualified name lookup (that is, if the function's name appears to the right of the scope resolution operator ::)
由于您在 class B
中覆盖 show()
,pA->show()
将在 class B
中调用 show()
.
希望这对您有所帮助
你通过覆盖虚函数和指针来实现多态性:
在您的示例中,您以多态方式使用了 pA,因此它是指向基 class (A) 的指针,但是您却向它分配了一个 class B 的对象,该对象是 A 的子对象。
并且您将 Show() 声明为虚拟的。
这是多态的主要目标;这意味着我们直到运行时才知道基指针指向的对象类型,例如在 main:
int main()
{
int choice;
cout << "1: A object 2: B object:\n\n";
cin >> choice;
if(1 == choice)
pA = new A; // result will be 1
else
if(2 == choice)
pA = new B; // result will be 2
if(pA)
pA->show();
delete pA;
pA = NULL;
// B* pB = new A; // error: cannot convert from class A* to class B* because C++ is not contravariant
B* pB = new B;
pB->show(); // result: 2
delete pB;
pB = NULL;
return 0;
}
我想了解 C++ 中虚函数的概念,我在网上阅读了它,但我无法理解为什么下面的程序输出是 2 而不是 1?谁能解释一下?
Class A
{
int a;
public:
A()
{
a = 1;
}
virtual void show()
{
cout <<a;
}
};
Class B: public A
{
int b;
public:
B()
{
b = 2;
}
virtual void show()
{
cout <<b;
}
};
int main()
{
A *pA;
B oB;
pA = &oB;
pA->show();
return 0;
}
Virtual functions are member functions whose behavior can be overridden in derived classes. As opposed to non-virtual functions, the overridden behavior is preserved even if there is no compile-time information about the actual type of the class. If a derived class is handled using pointer or reference to the base class, a call to an overridden virtual function would invoke the behavior defined in the derived class. This behavior is suppressed if the function is selected using qualified name lookup (that is, if the function's name appears to the right of the scope resolution operator ::)
由于您在 class B
中覆盖 show()
,pA->show()
将在 class B
中调用 show()
.
希望这对您有所帮助
你通过覆盖虚函数和指针来实现多态性: 在您的示例中,您以多态方式使用了 pA,因此它是指向基 class (A) 的指针,但是您却向它分配了一个 class B 的对象,该对象是 A 的子对象。 并且您将 Show() 声明为虚拟的。
这是多态的主要目标;这意味着我们直到运行时才知道基指针指向的对象类型,例如在 main:
int main()
{
int choice;
cout << "1: A object 2: B object:\n\n";
cin >> choice;
if(1 == choice)
pA = new A; // result will be 1
else
if(2 == choice)
pA = new B; // result will be 2
if(pA)
pA->show();
delete pA;
pA = NULL;
// B* pB = new A; // error: cannot convert from class A* to class B* because C++ is not contravariant
B* pB = new B;
pB->show(); // result: 2
delete pB;
pB = NULL;
return 0;
}