#include <iostream.h>class Base
{
public:
virtual Base *afn()//***************************
{
cout << "This is Base Class. \n" ;
return this;
}};class SubClass : public Base
{
public:
SubClass * afn()//***************************
{
cout << "This is a SubClass. \n";
return this;
}
};void test (Base & x)
{
Base *b;
b = x.afn();
}void main(void)
{
Base bc;
SubClass sc;
test(bc);
test(sc);
}
{
public:
virtual Base *afn()//***************************
{
cout << "This is Base Class. \n" ;
return this;
}};class SubClass : public Base
{
public:
SubClass * afn()//***************************
{
cout << "This is a SubClass. \n";
return this;
}
};void test (Base & x)
{
Base *b;
b = x.afn();
}void main(void)
{
Base bc;
SubClass sc;
test(bc);
test(sc);
}
解决方案 »
- 为什么我弹出的对话框得不到鼠标消息?
- 关于CREATEPROCESS()的问题
- 在两个鼠标按钮的消息间通信
- 急,COM组件的关闭问题..
- 完成端口的讨论组,有兴趣的请加入QQ群:3911737
- 用MFC写TCP通讯的程序,我使用的类派生于CSocket,当网络上有数据发过来的时候,在什么情况下无法触发OnReceive?
- 各位大虾,帮帮忙吧,在windows2000平台上通过vc++编程怎么才能给某一个文档加上摘要信息?
- 怎么办????????????????????????????????
- 如何用程序将一个屏保文件设成当前默认屏保?
- 哪里有Microsoft Directx Media 6.0 SDK下载啊?
- MDI,如何避免新建多个子窗口?
- WinApp::OnPaint()\WinMainFrame::OnPaint()\WinView::OnPaint()\WinView::OnDraw()有什么区别?
D:\03_Practice\2004-03-17\ch16_5\ch16_5.cpp(4) : see declaration of 'Base'
子类的函数afn的返回值与父类的不同
'class1::function1' : overriding virtual function differs from 'class2::function2' only by return type or calling conventionThe specified virtual function and a derived overriding function had identical parameter lists but different return types.An overriding function in a derived class cannot be redefined to differ only by its return type from a virtual function in a base class.A function in a derived class or structure overrides a virtual function in a base class only if their names, parameters, and return values are all identical. (If the functions have different parameters, the compiler treats them as different functions and does not override the virtual function.)It may be necessary to cast the return value after the virtual function has been called.The following is an example of this error:struct X
{
virtual void func();
};
struct Y : X
{
char func(); // error
};说的很清楚
你将子类函数定义改回来才是虚函数重写,否则编译器认为是重载函数
class SubClass : public Base
{
public:
SubClass * afn()//***************************
{
cout << "This is a SubClass. \n";
return this;
}
};
改为
class SubClass : public Base
{
public:
Base* afn()//***************************
{
cout << "This is a SubClass. \n";
return this;
}
};这样才是
{
public:
virtual Base *afn()//***************************
{
cout << "This is Base Class. \n" ;
return this;
}};class SubClass : public Base
{
public:
Base * afn()//改了这里
{
cout << "This is a SubClass. \n";
return this;
}
};void test (Base & x)
{
Base *b;
b = x.afn();
}void main(void)
{
Base bc;
SubClass sc;
test(bc);
test(sc);
}
你是想test多态吧,用基类的指针指向了派生类的对象。Visual是对的,但overide后的visual funcation 要参数和返回的完全匹配。
class Base
{
public:
virtual Base *afn()//
{
cout << "This is Base Class. \n" ;
return this;
}};
class SubClass : public Base
{
public:
Base * afn()//
{
cout << "This is a SubClass. \n";
return this;
}
};
void Factory (Base & x)
{
Base *b;
b = x.afn();
}
void main(void)
{
Base bc;
SubClass sc;
Factory (bc);
Factory (sc);
}