考虑以下内容。
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>
等等
为什么不使用模板?
constexpr int nfo[]{ 0, 1, 2, 3 };
template <int N>
class Haha {
public:
const static int BOOM = nfo[ N ];
};
int main() {
std::cout << Haha<2>::BOOM << std::endl; // 2
}