导数使用基函数而不是自己的函数
Derivative use base function instead of own function
我有一个base
类:
base.cpp:
#include "base.h"
base::base()
{
}
base::~base() {
}
void base::baseMethod(int a)
{
std::cout<<"base::baseMethod : "<<a<<std::endl;
}
base.h
#ifndef BASE_H
#define BASE_H
#include <iostream>
class base {
public:
base();
base(const base& orig);
virtual ~base();
void baseMethod(int);
private:
};
#endif /* BASE_H */
我有derivative
类从基
derivative.cpp
#include "derivative.h"
derivative::derivative() : base(){
}
derivative::~derivative() {
}
void derivative::baseMethod(int a)
{
std::cout<<"derivative::baseMethod : "<<a<<std::endl;
}
void derivative::derivativeMethod(int a)
{
baseMethod(a);
derivative::baseMethod(a);
}
derivative.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。但是当我调用derivatives emethod时,我使用的是基类的baseMethod。为什么呢?我如何调用派生类的baseMethod ?谢谢。
我使用Netbeans 8.2
, Windows 7 x64
, g++ 5.3.0 (mingw)
您需要在基类中设置baseMethod
virtual
:
virtual void baseMethod(int);
您不需要在子类中"重新确认"virtual
性,但有些人为了清晰起见这样做。(也包括子类中的析构函数)。
相关文章:
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- 是否可以在不填充自己的参数的情况下将模板函数作为参数传递?
- 在C++中,我可以在定义自己的复制构造函数后跳过定义赋值运算符吗?
- 在类自己的成员函数中构造类时,如何强制类模板参数推导?
- 在这种情况下,工会成员会调用自己的析构函数吗
- 将自己的函数添加到 ifstream
- 类中的枚举在调用自己的函数时不会改变
- 继承:子项使用父虚函数而不是自己的函数
- 如何在自己的函数中最好地处理Mat中的不同数字格式
- C++我自己的函数进行向量排序
- 是否可以阻止派生类调用自己的函数?(C++)
- 我应该如何在我自己的函数中传递 boost::asio::yield_context
- 为我自己的函数执行通常的算术转换
- 如何在CMake for OpenCV中使用我自己的函数
- 我已经寻找了MPI函数Scatterv的实现,但是我发现了任何可以帮助我编写自己的函数的东西
- 导数使用基函数而不是自己的函数
- 为什么Python运行c++函数比c++通过main()函数运行自己的函数要快?
- 将此指针弱传递给其自己的函数
- 我不知道如何调用自己的函数!C++
- 如何在c++和OpenGL中配置我自己的函数