[英]C++ static const members overriding
考虑以下。
struct A {
static const int X = 1;
static void printX() {std::cout << "X " << X << std::endl; };
};
struct B: public A {
static const int X = 2;
};
int main(argc argv){
B b;
b.printX();
}
如何强制b.printX()
打印值 2?
两者 - 常量和方法 - 必须是静态的。 因此,虚拟方法不适合。
对于那些认为他们比我更了解我的任务并希望看到我重新思考它的人,我会解释我努力的目标:)
想象一下具有基于一组静态常量的行为的类。 实现具有不同常量集并因此具有不同行为的子类的最简单方法是从具有特定常量值集的前一个类派生。 可以使用虚函数来解决该任务。 当然有可能,毫无疑问。 但是这个解决方案在符合建模实体的理论的意义上不是很纯粹。 在这种情况下,虚拟方法的使用比正确实现更像是一种技巧。
例如,IR 通道具有不同的脉冲持续时间和封装结构时序。 使用一组特定的常量值定义一组子类(不同的 IR 通道实现)很方便。 该值是静态的,因为它们对于 class 和 const 的每个对象都是通用的,因为它们仅在编译时需要。 并且由于基类和子类的内部实现略有不同,因此它们之间的最佳关系是super class - child class
。
现在是我原来问题的理由吗?
正如您将看到的,您将需要一个模板,并更改继承以使用该模板。 诀窍是不管派生类是否有一个 X 来掩盖基类 X 是否都起作用。
template<class C>
struct A {
static const int X = 1;
template<typename T>
static int getX(decltype(T::X)*)
{ return T::X; }
template<typename T>
static void getX(...)
{ return X; }
static void printX()
{ std::cout << "X " << getX<C>(0) << std::endl; }
};
struct B: public A<B> {
static const int X = 2;
};
struct B2: public A<B2> {
// No X
};
int main(){
B b;
b.printX(); // Prints X 2
B2 b2;
b2.printX(); // Prints X 1
}
只需将 X 的值设为模板参数:
#include <iostream>
template<int XValue=1>
struct A {
static const int X = XValue;
static void printX() {std::cout << "X " << X << std::endl; };
};
template<int XValue=2>
struct B: public A<XValue> {
};
struct C: public B<3> {
};
int main(int, char**){
B<> b;
b.printX();
}
根据定义,您对静态成员所做的任何事情都将“掩盖”,而不是“覆盖”。 您可以在“B”中重新实现“printX()”,但您不会真正覆盖该行为; 因为这会使用遮蔽,所以行为将完全取决于编译时类型,而不是运行时类型。
而不是使用static
和template
,我只会使用常规的常量属性和构造函数。
例如:
#include <iostream>
struct A {
A(const char* fn, const int X) : filename(fn), X(X) {};
void print() { std::cout << "X = " << X << ", FN = " << filename << std::endl; };
protected:
const char* filename;
const int X;
};
struct B : public A {
B() : A("data.dat", 5) {};
};
int main(int, char **) {
B b;
b.print();
}
在功能上,它完全符合您的要求。 输出:
X = 5, FN = data.dat
— 现在是编译器的工作来优化这些常量。 如果您不打算使用数以千计的对象B
,那么将这些常量设为static
可能不值得担心。
简短的回答:你不能。
稍微长一点,更复杂的答案:嗯,也许你可以。 带模板!
#include <iostream>
template <typename T> struct A
{
static const int X = 1;
static void printX()
{
std::cout << "X=" << T::X << std::endl;
}
};
struct B : public A<B>
{
static const int X = 2;
};
int main(int, char **)
{
B b;
b.printX();
return 0;
}
好吧,我会一起玩……你想把这个嵌套多一层深。 美好的。
#include <iostream>
template <int XX> struct T
{
static const int X = XX;
static void printX()
{
std::cout << "X=" << X << std::endl;
}
};
struct AA
{
static const int X = 1;
/* all other members go here */
};
struct A : public AA, public T<AA::X>
{
/* empty - put stuff in AA instead */
};
struct BB : public AA
{
static const int X = 2;
};
struct B : public BB, public T<BB::X>
{
};
struct CC : public BB
{
static const int X = 3;
};
struct C : public CC, public T<CC::X>
{
};
struct DD : public CC
{
static const int X = 4;
};
struct D : public DD, public T<DD::X>
{
};
int main(int, char **)
{
A a;
B b;
C c;
D d;
a.printX();
b.printX();
c.printX();
d.printX();
return 0;
}
你甚至可以跳过static const int X = ...;
在每个班级中,只需根据需要执行public T<1>
、 public T<2>
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.