简体   繁体   English

C ++重载运算符=指针无法编译

[英]C++ Overload Operator = for Pointers does not compile

I am trying to implement a template Class with an Operator Overload for = so far it works for non pointer elements, but if I try it for pointers it does not compile 我试图用=的操作符重载实现模板类,到目前为止,它适用于非指针元素,但是如果我尝试将其用于指针,它将无法编译

My template Class: 我的模板类:

template <class T>
class IBag {
public:
    T _val;
    void Set(T val) { _val = val; } 
    T Get() { return _val; }

    IBag& operator=(T val) {
        this->Set(val);
        return *this;
    }

    operator T() {
        return this->Get();
    }
};

How it works using it: 如何使用它:

class IBagExample
{
    static void showExample() {
        IBag<QString>* bag = new IBag<QString>();
        bag->Set(QString("Blub"));
        *bag = QString("Blub");
    }
};

how it does not compile: 如何不编译:

class IBagExample
{
    static void showExample() {
        IBag<QString*>* pbag = new IBag<QString*>();
        pbag->Set(new QString("Blub")); // This works
        pbag = new QString("Blub"); // This doesn't works ?!?
    }
};

The compiler Error I get is : 我得到的编译器错误是:

error: assigning to 'IBag<QString *> *' from incompatible type 'QString *'
    pbag = new QString("Blub");
         ^ ~~~~~~~~~~~~~~~~~~~

So my question is how do I get this work, I expected that the overload of the = operator will force c++ to pipe all requests including the new request to my custom function if its not the new constructor of the current class (so IBag* x = new IBag() will allocate IBag but x = new QString("sdjf"); will be forwarded to my custom overload as it is a different Object) 所以我的问题是我该如何进行这项工作,我希望=运算符的重载将强制c ++将包括新请求在内的所有请求通过管道传递给我的自定义函数(如果它不是当前类的新构造函数)(因此IBag * x = new IBag()将分配IBag,但x = new QString(“ sdjf”);将转发到我的自定义重载,因为它是另一个Object)

(Note:The IBag example is just a simplification of the Code I am trying to implement.) (注意:IBag示例只是我尝试实现的代码的简化。)

Thanks a lot, 非常感谢,

The error message says it well: 错误消息说得很好:

error: assigning to 'IBag<QString *> *' from incompatible type 'QString *'
    pbag = new QString("Blub");
         ^ ~~~~~~~~~~~~~~~~~~~

pbag is a pointer, not an IBag . pbag是一个指针,而不是IBag You overloaded operator = for IBag , not for IBag * (which is not possible). 您为IBag重载了operator = ,而不为IBag * (这是不可能的)。

This should compile because it calls your operator: 这应该编译,因为它会调用您的运算符:

*pbag = new QString("Blub");

Well

pbag = new QString("Blub"); // This doesn't works ?!?

try 尝试

*pbag = new QString("Blub");

Since pbag is of type IBag* which is not of type IBag . 由于pbagIBag*类型,而不是IBag类型。 You're esentially calling the assignment operator of IBag* which is built-in I guess but probably "looks" like IBag*上是在调用IBag*的赋值运算符,我猜它是内置的,但可能像

Ibag*& operator=(Ibag* const &);

which is not your IBag& operator=(T val); 不是您的IBag& operator=(T val); since that one is part of IBag not IBag* . 因为那是IBag一部分, IBag不是IBag*

You cannot alter the C++ pointer types and operator= must be a member function of the specified type. 您不能更改C ++指针类型,并且operator=必须是指定类型的成员函数。

and

IBag<QString> bag = QString("Blub");
IBag<QString*> pbag = new QString("Blub");

do not work because you don't have a custom constructor from T . 不起作用,因为您没有T的自定义构造函数。

After various changes in my Code using your suggestions and reviewing what I need, I have following Result : 在使用您的建议对我的代码进行了各种更改并查看了我的需要之后,我得到以下结果:

class IBagExample
{
   void showExample() {
        IBag<QString*> pbag;
        pbag = new QString("Blub"); // This works !
    }
};

This does compile and work, but what does not work is following and I will ask this in another follow up question: 这确实可以编译并起作用,但是下面是行不通的,我将在另一个后续问题中提出:

class IBagExample
{
   void showExample() {
        IBag<QString*> pbag = new QString("Blub"); // This doesn't work !
    }
};

So thanks a lot for your help! 因此,非常感谢您的帮助!

Follow up Question: C++ Overload Operator = for Pointers does not work/compile properly 后续问题: C ++重载运算符=指针无法正常工作/编译

You overload operator for a class to be used by an object. 您可以重载运算符,以供对象使用的类。 Here pBag is just a pointer and not an object. 这里的pBag只是​​一个指针,而不是一个对象。 C++ standard ISO/IEC 14882 § 13.5.3 mention an example which takes pointers, but used with de-referencing (by *). C ++标准ISO / IEC 14882§13.5.3提到了一个使用指针的示例,但该示例使用了取消引用(*)。

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

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