C++ 导数使用基函数而不是自身函数

C++ 导数使用基函数而不是自身函数,c++,inheritance,C++,Inheritance,我有一个base课程: base.cpp: #include "base.h" base::base() { } base::~base() { } void base::baseMethod(int a) { std::cout<<"base::baseMethod : "<<a<<std::endl; } 导数.h #ifndef DERIVATIVE_H #define DERIVATIVE_H #include "base.h"

我有一个
base
课程:

base.cpp:

#include "base.h"

base::base() 
{
}

base::~base() {
}

void base::baseMethod(int a) 
{
    std::cout<<"base::baseMethod : "<<a<<std::endl;
}
导数.h

#ifndef DERIVATIVE_H
#define DERIVATIVE_H

#include "base.h"

class derivative : public base{
public:
    derivative();
    derivative(const derivative& orig);
    virtual ~derivative();

    void derivativeMethod(int);
    void baseMethod(int);
private:

};

#endif /* DERIVATIVE_H */
main.cpp

derivative t;
t.baseMethod(1);
t.derivativeMethod(2);
输出为:

derivative::baseMethod : 1
base::baseMethod : 2
base::baseMethod : 2
当我用派生类对象调用baseMethod时,实际上我使用的是派生类的baseMethod。但当我调用derivetiveMethod时,我使用的是基类的baseMethod。为什么呢?如何调用派生类的baseMethod? 谢谢


我使用的是
Netbeans 8.2
windows7x64
g++5.3.0(mingw)

您需要在基类中创建
baseMethod
虚拟

virtualvoidbasemethod(int)


你不需要“重新确认”孩子课堂上的虚拟性,但有些人这样做是为了清楚。(这也包括子类中的析构函数)。

您没有将
baseMethod
标记为
virtual
。这不是问题,只是为了让您知道它被称为派生,而不是派生。当你派生一个类时,它就变成了一个派生类。我也不能复制,所有调用都是按预期调用
派生的
函数。你还可以“重新确认”派生类的函数用
重写
重写基类虚函数的事实。这比仅仅为了清晰起见而做强了一点。
derivative t;
t.baseMethod(1);
t.derivativeMethod(2);
derivative::baseMethod : 1
base::baseMethod : 2
base::baseMethod : 2