【发布时间】:2017-03-29 16:01:04
【问题描述】:
考虑以下代码:
#include <stdio.h>
#include <iostream>
/// Header-file
class Base {
public:
virtual void do_something() const =0;
int GetAttrib () const {return constattribute_;};
static const int constattribute_;
};
typedef Base* Derived_Ptr; //<< adress derived classes by their base-class ptr; so no templates for Base
class DerivedA : public Base {
// static const int constattribute_; //<< change this static attribute for all DerivedA class instances and their derivatives
void do_something() const {};
};
class DerivedB : public Base {
// static const int constattribute_; //<< change this static attribute for all DerivedB class instances and their derivatives
void do_something() const {};
};
/// CC-file
using namespace std;
const int Base::constattribute_(0);
const int DerivedA::constattribute_(1); //<<error: no such variable 'constattribute' in class DerivedA
const int DerivedB::constattribute_(2); //<<error: no such variable 'constattribute' in class DerivedB
int main(void) {
Derived_Ptr derivedA = new DerivedA();
Derived_Ptr derivedB = new DerivedB();
cout << derivedA->GetAttrib() << derivedB->GetAttrib() <<endl;
return 0;
};
我的意图是我有一些抽象接口(Base),它还定义了一个变量,它应该存在于所有派生类中,并且是可检索的。所有类型的子类都应该被强制/能够重新定义它们的特定值,最好是在类声明期间(毕竟在声明类时这些值是已知的)。
我想实现代码,而不是更改 main() 程序,以便输出为“12”而不是现在(取消注释代码中的当前行)“00”(这样做会隐藏基类中的字段)。
我试图调查此事,有不同的解决方案路径,但其中许多与我的直觉相反: 1. 有些遵循 CRTP 模式,但是如果我想通过 main 中的 base-ptr 来处理我的子类,这是不可能的。 2.其他方案需要为每个派生实例虚拟化'GetAttrib()'函数,这很麻烦,并且修改属性的动作被隐藏在函数定义中。 3. 第三种可能性是删除静态模式并将“constattribute_”字段作为常规成员,但这迫使我将其作为参数拖过所有构造函数。
我很确定必须有一些更聪明的方法来做到这一点。任何提示表示赞赏。
【问题讨论】:
标签: c++ inheritance static field