#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);
}

解决方案 »

  1.   

    error C2555: 'SubClass::afn' : overriding virtual function differs from 'Base::afn' only by return type or calling convention
            D:\03_Practice\2004-03-17\ch16_5\ch16_5.cpp(4) : see declaration of 'Base'
      

  2.   

    虚函数必须保持参数和返回值一致
    子类的函数afn的返回值与父类的不同
      

  3.   

    Compiler Error C2555
    '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;
    }
    };这样才是
      

  4.   

    #include <iostream.h>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 test (Base & x)
    {
    Base *b;
    b = x.afn();
    }void main(void)
    {
    Base bc;
    SubClass sc;
    test(bc);
    test(sc);
    }
    你是想test多态吧,用基类的指针指向了派生类的对象。Visual是对的,但overide后的visual funcation 要参数和返回的完全匹配。
      

  5.   

    //factory#include <iostream.h>
    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);
    }