抽象基类的 QPointer

发布于 2024-11-01 04:12:12 字数 511 浏览 0 评论 0原文

我正在编写一个表达式解析库。 它是用 Qt 编写的,我有一个这样的类结构:
QCExpressionNode - 表达式所有部分的抽象基类
QCConstantNode - 表达式中的常量(扩展 QCExpressionNode
QCVariableNode - 表达式中的变量(扩展 QCExpressionNode
QCBinaryOperatorNode-二进制加法、减法、乘法、除法和幂运算符(扩展QCExpressionNode

我希望能够使用智能指针(例如QPointer 或 QSharedPointer),但我遇到了以下挑战:
-QPointer 可以与抽象类一起使用吗?如果是,请提供示例。
-如何将QPointer转换为具体的子类?

I am writing an expression-parsing library.
It is written with Qt, and I have a class structure like this:
QCExpressionNode-Abstract Base Class for all pieces of an expression
QCConstantNode-Constants in an expression (extends QCExpressionNode)
QCVariableNode-Variables in an expression (extends QCExpressionNode)
QCBinaryOperatorNode-Binary Addition, Subtraction, Multiplication, Division and Power operators (extends QCExpressionNode)

I would like to be able to use smart pointers (like QPointer or QSharedPointer), but I am running into the following challenges:
-Can a QPointer be used with abstract classes? If so, please provide examples.
-How to cast a QPointer to a concrete subclass?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-11-08 04:12:12

我不明白你有什么理由不能这样做。举个例子:

class Parent : public QObject
{
public:
   virtual void AbstractMethod() = 0;
};

class Child: public Parent
{
public:
   virtual void AbstractMethod() {  }

   QString PrintMessage() { return "This is really the Child Class"; }
};

现在像这样初始化一个 QPointer:

QPointer<Parent> pointer = new Child();

然后你可以像通常使用 QPointer 一样调用“抽象”类上的方法

pointer->AbstractMethod();

理想情况下,这就足够了,因为你可以使用你的抽象方法中定义的抽象方法来访问你需要的所有内容。父类。

但是,如果您确实需要区分子类或使用仅存在于子类中的内容,则可以使用dynamic_cast。

Child *_ChildInstance = dynamic_cast<Child *>(pointer.data());

// If _ChildInstance is NULL then pointer does not contain a Child
// but something else that inherits from Parent
if (_ChildInstance != NULL)
{
   // Call stuff in your child class
   _ChildInstance->PrintMessage();
}

我希望这有帮助。

额外注意:您还应该检查pointer.isNull()以确保QPointer确实包含某些内容。

I don't see any reason why you can't do this. Take this example:

class Parent : public QObject
{
public:
   virtual void AbstractMethod() = 0;
};

class Child: public Parent
{
public:
   virtual void AbstractMethod() {  }

   QString PrintMessage() { return "This is really the Child Class"; }
};

Now initialize a QPointer like this:

QPointer<Parent> pointer = new Child();

You can then call methods on the 'abstract' class as you would normally with a QPointer

pointer->AbstractMethod();

Ideally this would be enough, because you could just access everything you need with the abstract methods defined in your parent class.

However, if you really need to differentiate between your child classes or use something that only exists in the child class, you can use dynamic_cast.

Child *_ChildInstance = dynamic_cast<Child *>(pointer.data());

// If _ChildInstance is NULL then pointer does not contain a Child
// but something else that inherits from Parent
if (_ChildInstance != NULL)
{
   // Call stuff in your child class
   _ChildInstance->PrintMessage();
}

I hope that helps.

Extra note: You should also check pointer.isNull() to make sure that the QPointer actually contains something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文