2017-08-11 59 views
1

想象一下,我有两个相似的结构A和B.A和B都有一个指向A的指针,但A有一个指向A或B的附加指针。C++:使用基类Derived-class变量

我认为是这样的一个基地和一个派生类

template <bool T> struct Derived; 
struct Base { Derived<0> *p1; }; 
template <> struct Derived<0> : public Base { Base *p2; }; 
template <> struct Derived<1> : public Base {}; 

其中A是Derived<0>,B是Derived <1>

这里的问题是,当通过p2访问一个类时,编译器不知道它是哪个派生类,并且类似这样会给出一个错误。

Derived<0> x, y, z; 
x.p2 = &y; 
y.p2 = &z; 
x.p2->p2; // Error 

您是否有任何人知道任何神奇的解决方法,最好只使用编译时功能?

我也需要知道我使用的派生类型,以便我知道是否可以使用p2。

如果有帮助,你可以想像的事情作为一个双链表,其中Derived<0>是一个正常的节点,Derived<1>是终端节点,p1上一页指针和p2下一个指针。

编辑:它不需要使用Base-and-Derived-class-type结构,它可以是任何东西。

+2

这是一个错误,因为'x.p2'是一个指向'Base'的指针,它没有'p2'成员,所以' - > p2'不能解析任何东西。你可以'dynamic_cast >(x.p2)'并检查结果是不是'nullptr'。如果不是,则可以通过铸造的指针访问' - > p2'。 – cdhowie

+0

(请注意,对于'dynamic_cast'来说,基类必须是多态才能有条件地正确投射。) – cdhowie

回答

1

一个可能的解决方案是基于双重调度,这是同样的想法是最着名的访问者模式。

它遵循最小,工作示例:

#include<iostream> 

template<int> 
struct Derived; 

struct Visitor { 
    template<int N> 
    void visit(Derived<N> &); 
}; 

struct Base { 
    Derived<0> *p1; 
    virtual void accept(Visitor &) = 0; 
}; 

template<> 
struct Derived<0>: public Base { 
    void accept(Visitor &) override; 
    Base *p2; 
}; 

template<> 
struct Derived<1>: public Base { 
    void accept(Visitor &) override; 
}; 

template<> 
void Visitor::visit(Derived<0> &d) { 
    std::cout << "Derived<0>" << std::endl; 
    d.p2->accept(*this); 
} 

template<> 
void Visitor::visit(Derived<1> &) { 
    std::cout << "Derived<1>" << std::endl; 
} 

void Derived<0>::accept(Visitor &v) { 
    v.visit(*this); 
} 

void Derived<1>::accept(Visitor &v) { 
    v.visit(*this); 
} 

int main() { 
    Visitor v; 
    Derived<0> x, y; 
    Derived<1> z; 
    x.p2 = &y; 
    y.p2 = &z; 
    x.p2->accept(v); 
} 

见它,并在wandbox运行。

如果你可以使用C++ 17,因此std::variant,事情远远更简单:

#include<iostream> 
#include<variant> 

template<int> 
struct Derived; 

struct Base { 
    Derived<0> *p1; 
}; 

template<> 
struct Derived<0>: public Base { 
    std::variant<Derived<0> *, Derived<1> *> p2; 
}; 

template<> 
struct Derived<1>: public Base {}; 

struct Visitor { 
    void operator()(Derived<0> *d) { 
     std::cout << "Derived<0>" <<std::endl; 
     std::visit(*this, d->p2); 
    } 

    void operator()(Derived<1> *) { 
     std::cout << "Derived<1>" <<std::endl; 
    } 
}; 

int main() { 
    Visitor v; 
    Derived<0> x, y; 
    Derived<1> z; 
    x.p2 = &y; 
    y.p2 = &z; 
    std::visit(v, x.p2); 
} 

见它,并在wandbox运行。

+0

嗯,非常好的技巧。谢谢! – gmardau