我有一个课程Child延伸Base.在我的课堂上,Foo我的签名方法如下:
void Foo::doStuff(Base *& base); // case A
void Foo::doOther(Child *& child); // case B
void Foo::doSomething(Base * base); // case C
Run Code Online (Sandbox Code Playgroud)
我对案例B没有任何问题.如果我写:
Child * myChild = new Child();
Foo foo;
foo.doOther(myChild);
Run Code Online (Sandbox Code Playgroud)
没有编译/运行时错误.
问题是案例A,如果我写:
Child * test = new Child();
Foo foo;
foo.doStuff(test);
Run Code Online (Sandbox Code Playgroud)
我收到编译时错误消息:
错误:没有匹配函数来调用foo :: doStuff ...
如果我没有使用对指针的引用,则没有问题(案例C)
我还试图Base *&在调用之前将测试对象强制转换为对象doStuff,但我想让它像C一样工作.
有什么想法吗?
编辑:
它是:Child*myChild = new Child(); //不是Base*myChild = new Child();