我想知道是否有无论如何,我的超类可以调用子类的函数initValues()而不必重写构造函数?从超类访问子类?
下面的代码:
#ifndef VECTOR_MATH_H
#define VECTOR_MATH_H
#include "GL\glew.h"
#include <iostream>
namespace Math3d
{
class Vector
{
public:
Vector(int length=2) : v(new float[length]) { initValues(); }
~Vector() { delete[] v; }
protected:
virtual void initValues()
{
std::cout << "Vector" << std::endl;
}
float* v;
};
class Vector3 : public Vector
{
public:
protected:
void initValues()
{
std::cout << "Vector3" << std::endl;
}
};
}
#endif
然后我创建这样一个变量: 的Vector3 VEC;
然后我想要调用子类Vector3的initValues() 方法 。
这可能吗?
似乎是重复的:http:// stackoverflow。com/questions/7644154/calling-base-class-virtual-method-by-derived-class-virtual-method –
@StevenBehnke看起来像OP的另一种要求。 –
它可能是,但反过来对我没有任何意义。 Vector如何知道派生类? –