Are virtual functions the only way to achieve Runtime Polymorphism in C++?
问题 One of my friends asked me "How Runtime Polymorphism is achieved in C++?" I answered "By Inheritance" He said "No, it can be achieved only using virtual functions". So I gave him an example of the following code :- #include<iostream> using namespace std; class A { public: int i; A(){i=100;} }; class B : public A { public: int j; B(){i = -1; j = 99;} }; void func(A& myA) { cout<<myA.i << endl; } int main() { B b; A* a = new B(); func(*a); func(b); delete a; return 0; } Here, function func()