gpt4 book ai didi

c++ - 检查 RTTI

转载 作者:太空宇宙 更新时间:2023-11-04 14:49:55 27 4
gpt4 key购买 nike

<分区>

我有以下类和方法:

//Base class
class Node {
public:
virtual ~Node() {};
Node() {};
private:
// Private things for my implementation.
};

class Element : public Node {
public:
// Returns the name of the element.
const xml::String &name() const {
return eleName;
}

static bool is_Element(const Node *base) {
Element *p = NULL;
p = dynamic_cast<Element*>(base);
return (p!=NULL);
}

static const Element *to_Element(const Node *base) {
return dynamic_cast<Element*>(base);
}

private:
s_namespace eleNamespace;
xml::String &eleName;
// Private things for my implementation.
};

这里,当我进行动态转换时,它会提示以下编译错误。如何纠正?一种方法是简单地删除参数的 const。但我认为这不是正确的方法。

oops.cpp: In static member function ‘static bool xml::Element::is_Element(const xml::Node*)’: oops.cpp:208:44: error: cannot dynamic_cast ‘base’ (of type ‘const class xml::Node*’) to type ‘class xml::Element*’ (conversion casts away constness) oops.cpp: In static member function ‘static const xml::Element* xml::Element::to_Element(const xml::Node*)’: oops.cpp:213:47: error: cannot dynamic_cast ‘base’ (of type ‘const class xml::Node*’) to type ‘class xml::Element*’ (conversion casts away constness)

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com