我有以下程序,其中Derived类继承自Base类。
class Base
{
int p_var;
public:
virtual void function()
{
cout << "Function() of class Base"<<endl;
}
};
class Derived: public Base
{
public:
void function()
{
cout << "Function() of class Derived"<<endl;
}
};
class Another
{
int a_var;
friend void function();
};
void function(Base& base, Another& anot)
{
anot.a_var=base.p_var;
}
int main()
{
Base *bptr,bobj;
Derived dobj;
bptr=&bobj;
bptr->function();
bptr=&dobj;
bptr->function();
cin.get();
return 0;
}
在上面的例子中,我想让“function()”成为“Base”类的虚拟成员函数,将朋友改为“Another”类。语法应该如何?