I would like to know whether c++ guarantees down-casting grandmother base class to grand child class like curiously recurring template pattern. The following code works well in my environment. However I am not sure it works any conditions/environments. Please tell me what you know. Thank you very much.
PS. Honestly said, I am not sure my way of asking stack overflow is ok or bad. If you find the bad points, please let me know. Thank you again.
#include <iostream>
template <typename GrandChild>
class GrandBase {
public:
void print(void) {
const GrandChild* grand_child = static_cast<const GrandChild*>(this);
std::cout << "GrandChild::value = " << grand_child->grand_child_value << std::endl;
}
};
template <typename Derived>
class BridgeClass : public GrandBase<Derived> {};
class GrandChild : public BridgeClass<GrandChild> {
public:
GrandChild() = default;
virtual ~GrandChild() = default;
int grand_child_value = 17;
};
void test_down_casting_to_grand_base(void) {
GrandChild a;
a.print();
}
int main(int argc, char **argv) {
test_down_casting_to_grand_base();
return 0;
}
The output is
GrandChild::value = 17