为什么派生类无法访问非虚拟基类函数?
Why won't derived class access a non-virtual base class function?
我在使派生类访问基类中定义的函数时遇到问题。 基类头文件,称为Particle.h,是:
class Particle {
protected:
vector<double> x_,y_,z_;
// lots of other protected members that are not relevant to the question
public:
// constructors and other functions not relevant to question are omitted
virtual double getX(const unsigned int index, const double theta, const double phi);
virtual vector<double> getX(const double theta, double Real phi);
vector<double> getX(const unsigned int surfindex);
}
此函数的定义位于名为 Particle.cc 的文件中:
#include "Particle.h"
vector<double> Particle::getX(const unsigned int surfindex)
{
vector<double> xp;
xp.clear();
xp.resize(3,0.0);
xp[0] = x_.at(surfindex);
xp[1] = y_.at(surfindex);
xp[2] = z_.at(surfindex);
return xp;
}
派生的类头文件称为 Star.h,是:
#include "Particle.h"
using namespace std;
class Star : public Particle {
public:
// constructors and other functions not relevant to question are omitted here
virtual void computeRoundness(const unsigned int method);
double getX(const unsigned int index, const double theta, const double phi);
vector<double> getX(const double theta, double Real phi);
}
computeRoundness 函数的定义位于一个名为 Star.cc 的文件中:
#include "Star.h"
// Lots of other function definitions not relevant to question are omitted here
void Star::computeRoundness(const unsigned int method)
{
vector<double> X;
unsigned int count = 0;
while (count < ntheta) {
X = getX(count); // Should call base class function, right?
// do some more things with X that are not relevant to this question
}
}
但是我收到这个编译时错误:
Star.cc: In member function ‘virtual void Star::computeRoundness(unsigned int)’:
Star.cc:1340: error: no matching function for call to ‘Star::getX(unsigned int&)’
Star.h:687: note: candidates are: virtual double Star::getX(unsigned int, double, double)
Star.h:696: note: virtual std::vector<double, std::allocator<double> > Star::getX(double, double)
我过去曾成功地从其他C++项目中的派生类调用基类函数,所以我一定在这里忽略了一些简单的东西,但我就是找不到它。 我认为基类函数应该由派生类继承,除非它们被声明为虚拟然后在派生类中被覆盖(但这里不是这种情况(,即使派生类重载函数名称,就像我在这里做过几次一样。 难道不是这样吗? 如果没有,我能做些什么来解决这个问题,而不仅仅是在我的派生类中重新定义相同的函数?
非常感谢您的任何帮助。
您必须在派生类中添加using getX;
,或者在函数中使用particle::getX
。
该标准规定,如果派生函数具有同名的函数,则不会自动使用基类的函数 - 即使派生的函数不适合。这是为了防止错误。
你必须告诉派生类它将使用基类的函数(通过using getX;
direvtive(或显式调用基类的函数(通过调用particle::getX(...)
函数 getX 在基类中的声明为:
virtual vector<double> getX(const double theta, double Real phi);
但定义是:
vector<double> Particle::getX(const unsigned int surfindex)
{
//Stuff
}
签名不匹配! 更改声明,代码将起作用
顺便说一句,一个矢量的副本非常昂贵,请考虑使用参考更改您的设计
答案是这样的:如果基类具有函数的重载版本"n" 个和在派生类中,您可以重新定义/覆盖(更改主体(或重载(更改签名(,然后只有派生类中的该版本可供您使用。
样本:
class Base
{
public:
int abc(){}
float abc(){}
};
class Der:public Base
{
public:
SomeClassObj abc(){} //int and float returning versions of abc are now hidden for Der object
};
覆盖/重定义也是如此。
相关文章:
- 子类地址等于虚拟基类地址?
- 虚拟基类函数中派生类的大小
- 虚拟基类初始化
- 虚拟基类在内部如何工作?编译器如何解析对基方法的调用?
- 返回纯虚拟基类的向量的元素
- std::is_base_of和虚拟基类
- 如何在子类中重写时调用私有虚拟基类实现
- 从两个不同类继承的非虚拟基类的访问成员
- 为什么必须由最派生的类构建虚拟基类
- 虚拟基类的派生类列表
- boost::bind 无法绑定到纯虚拟基类中定义的非静态函数模板成员类型
- 虚拟基类和继承
- (为什么)纯虚拟派生类中是否需要虚拟基类构造函数调用?
- 在派生类中使用虚拟基类的受保护的CTOR
- 私有非虚拟基类函数被称为派生类中的函数
- 虚拟基类的创建顺序
- 使用虚拟基类C++向下转换
- 为什么在派生最多的类的初始值设定项列表中显式调用虚拟基类构造函数的规则,而较老的祖先已经拥有它?
- 虚拟与纯虚拟基类函数并从DLL导出
- 在多级继承中派生的虚拟基类会发生什么