简体   繁体   English

这里虚拟运算符()()的目的是什么?

[英]What is the purpose of a virtual operator ()() here?

I'm trying to modify code I found , but I'm blocked by my lack of understanding of the purpose, importance, and/or relevance of this virtual operator: 我正在尝试修改我找到的代码 ,但是由于我对这个虚拟运算符的目的,重要性和/或相关性缺乏了解而阻止了我:

  1. Can someone provide insight as to why this operator is necessary or useful? 有人能否提供有关此运算符为何必要或有用的信息?
  2. Am I right in thinking it takes parentItem() , rect_ , and resizer_ as parameters, then modifies the value of resizer_ ? 我认为它需要将parentItem()rect_resizer_作为参数,然后修改resizer_的值吗?

Constructor in .h: .h中的构造函数:

virtual void operator()(QGraphicsItem* item, const QRectF& rect) = 0;

Call in .cpp: 打电话给.cpp:

(*resizer_)(parentItem(), rect_);

Trimmed context for the constructor for reference: 修剪构造函数的上下文以供参考:

class SizeGripItem : public QGraphicsItem
{
    private:

        class HandleItem : public QGraphicsRectItem
        {
            public:
                HandleItem(int positionFlags, SizeGripItem* parent);

            private:    
                SizeGripItem* parent_;
        };

    public:
        class Resizer
        {
            public:
                virtual void operator()(QGraphicsItem* item,
                                        const QRectF& rect) = 0;
        };

        SizeGripItem(Resizer* resizer = 0, QGraphicsItem* parent = 0);
        virtual ~SizeGripItem();

    private:
        void doResize();
        QRectF rect_;
        Resizer* resizer_;
};

The Resizer is a broken attempt at a polymorphic functor (function object). Resizer是对多态仿函数 (函数对象)的破坏尝试。 Such an idiom was useful before C++11. 在C ++ 11之前,这样的习惯用法很有用。 It's broken because such functors are useless without a virtual destructor. 它被打破了,因为没有虚拟析构函数,这些函子是没用的。 It should have looked as follows: 它应该看起来如下:

class Resizer {
public:
  virtual void operator()(QGraphicsItem* item, const QRectF& rect) = 0;
  virtual ~Resizer() {}
};

Such objects are callable: 这些对象是可调用的:

void invokeResizer(Resizer * resizer, QGraphicsItem * item, const QRectF & rect) {
  (*resizer)(item, rect);
}

The above will execute the method operator()(QGraphicsItem*,const QRectF&) on the resizer object. 上述将执行方法operator()(QGraphicsItem*,const QRectF&)的上resizer对象。

In modern code, instead of such hacks, one should use std::function<void(QGraphicsItem*, const QRectF &)> . 在现代代码中,应该使用std::function<void(QGraphicsItem*, const QRectF &)>而不是这样的hack。

Regarding point 2, consider this line: 关于第2点,请考虑以下这一行:

(*resizer_)(parentItem(), rect_);

resizer_ is likely a pointer to an object of an unknown type T , thus *resizer is a reference to an object of the same type T . resizer_可能是指向未知类型T的对象的指针,因此*resizer是对相同类型T的对象的引用。
If it has a definition for operator() that accepts two parameters having types (let me say) decltype(parentItem()) and decltype(rect_) , you can invoke it as it happens in the example. 如果它有一个operator()的定义,它接受两个带有类型(让我说) decltype(parentItem())decltype(rect_) ,你可以在示例中调用它。
In other terms, it's equivalent to: 换句话说,它相当于:

resizer_->operator()(parentItem(), rect_);

It doesn't modify the value of resizer_ at all. 它根本不会修改resizer_的值。

Can someone provide insight as to why this operator is necessary or useful? 有人能否提供有关此运算符为何必要或有用的信息?

Well, it mostly depends on the context and the actual problem it aims to solve. 那么,它主要取决于背景和它要解决的实际问题。
It's hard to say from a line of code. 从一行代码中很难说。
If you find it useless, don't use it. 如果您觉得它没用,请不要使用它。 That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM