[英]Calling derived class function from base class with templates without virtual
I need to generate some members/methods of a class with a script.我需要用脚本生成类的一些成员/方法。 I'm trying to break up this class in two, with base class being generated members, and derived class having hand coded members.我试图将这个类分成两个,基类是生成的成员,派生类具有手工编码的成员。 However, I'm getting stuck in figuring out how to call derived member function D::f2()
from the base class B::f1()
.但是,我一直在弄清楚如何从基类B::f1()
调用派生成员函数D::f2()
B::f1()
。
Here is the simplified code:这是简化的代码:
#include <cstdio>
template <typename _T>
class B {
public:
void f3() {
puts("okay");
}
void f1() {
f2(); // What C++ Magic to call f2() properly !!!
}
};
class D : public B<D> {
public:
void f2() {
f3();
}
};
int main() {
D d;
d.f1();
}
Is there any way, I can call D::f2()
from B::f1()
without using virtual functions ?有什么办法,我可以不使用虚函数从B::f1()
调用D::f2()
吗?
Added later:稍后补充:
If we do pointer manipulation, we will end up with injection, and I understand it's not a good idea, and I'll take the advice of not doing it.如果我们进行指针操作,我们最终会进行注入,我知道这不是一个好主意,我会听取建议不要这样做。 Let's stop that thread.让我们停止那个线程。
I am trying to find a solution using template only.我正在尝试仅使用模板找到解决方案。 I can generate any complex thing for the generated code.我可以为生成的代码生成任何复杂的东西。 It can even be a several functors etc. However the hand coded written part should be hand-codable.它甚至可以是几个函子等。然而,手写部分应该是可手写的。
If you really really really want to do it:如果你真的真的很想这样做:
static_cast<_T*>(this)->f2();
As people have mentioned, this is the curiously recuring template pattern !正如人们所提到的,这是奇怪的重复模板模式!
This is a typical Curiously recuring template pattern .这是一个典型的Curiously recurring 模板模式。 You can do:你可以这样做:
template <typename _T>
class B {
public:
void f3() {
puts("okay");
}
void f1() {
static_cast<_T>(this)->f2();
}
};
A base class should have no notion of its children.基类不应该有其子类的概念。 Attempt to call a child method from base class is a sure sign of bad architecture.尝试从基类调用子方法是不良架构的明确标志。 Virtual functions will not help you here by the way.顺便说一下,虚拟函数在这里帮不了你。
If you have to call a child's function from the base class you can do so just like you would do from any other function in your code.如果您必须从基类调用子函数,您可以像在代码中的任何其他函数中一样这样做。 Still you will need an instance of the base class to call it on or alternatively you will have to static_cast
the this
pointer to a pointer of the child class type.您仍然需要一个基类的实例来调用它,或者您必须将this
指针static_cast
指向子类类型的指针。 This is a very bad idea!这是一个非常糟糕的主意!
There is another way, but at the cost of an ugly C style/reinterpret cast:还有另一种方法,但代价是丑陋的 C 风格/重新解释演员:
#include <cstdio>
#include <functional>
class B {
public:
typedef std::function<void (B*)> Function;
void f3() {
puts("okay");
}
void f1() {
_func(this);
}
Function _func;
};
class D : public B
{
public:
D()
{
_func = (void (B::*)()) &D::f2; // Here is the awfull cast I hate to do
}
void f2() {
f3();
}
};
int main() {
D d;
d.f1();
}
http://ideone.com/yOR0xT http://ideone.com/yOR0xT
I find this a lot less clean than the CRTP , because you have no compile time check...我发现这比CRTP少得多,因为您没有编译时检查...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.