简体   繁体   English

c++ 使用指向基类的指针在派生类中重载运算符

[英]c++ Operator overloading in derivated class using pointers to base class

I'm writing a class in c++ for an embedded system that behaves like a variable of multiple types (int, float, string, etc).我正在用 C++ 为嵌入式系统编写一个该类的行为类似于多种类型(int、float、string 等)的变量 By means of subclasses that handle each type of value.通过处理每种类型值的子类。 I need to do operator overloading for several operators like (+ - * / =! = + = - = == > < && ||!) But perform the operations from a pointer to the base class .我需要对几个运算符(如 (+ - * / =! = + = - = == > < && ||!) 进行运算符重载)但是从指向基类指针执行操作。 How should I write these functions?我应该如何编写这些函数? I'm testing something like this, but it does not do the sum.我正在测试这样的东西,但它并没有做总和。

class base
  { 
    public:
    virtual base& operator=(const base& other) {};
    virtual base& operator+(const base& other) {};
 ...
  }

class VarInt: public base
{
 public:
    int value;
    VarInt& operator=(const VarInt& other);
    VarInt& operator+(const VarInt& other);
}

class VarFloat : public base
{
 public:
    float value;
  ...
}

main(){
 ...
    base* varbase1 = new VarInt();
    base* varbase2 = new VarInt();
    *varbase1 = 1;
    *varbase2 = 2;
    *varbase1 += *varbase2;
}

Your problem lies in the assumption that when VarInt inherits from base , then:您的问题在于假设当VarIntbase继承时,则:

virtual base& operator = (const base& other) {};

and

virtual VarInt& operator = (const VarInt& other) {};

are the the exact same thing.是完全一样的东西。 Unfortunately, they are not .不幸的是,它们不是 What you do here, is hiding the previous method.你在这里所做的,就是隐藏之前的方法。 When you override a virtual function you need to specify it the exact same way it was firstly declared, that is:当您override一个虚函数时,您需要以与首次声明完全相同的方式指定它,即:

virtual base& operator = (const base& other) {};

The returned base& can be bounded to VarInt object, so we are safe here.返回的base&可以绑定到VarInt对象,所以我们在这里是安全的。 The provided argument, being const base& can also be referenced to a VarInt object.提供的参数const base&也可以引用到VarInt对象。 Now you have to think about what you actually want to achieve here.现在您必须考虑您在这里真正想要实现的目标。 Assuming that from your base class multiple classes with different functionalities can inherit, what will happen if you, for example, try to add VarFloat to VarInt ?假设从您的base类可以继承多个具有不同功能的类,例如,如果您尝试将VarFloat添加到VarInt会发生什么? What is the expected result?预期的结果是什么? Compiler error?编译器错误? Throwing an exception?抛出异常?

If you really wish to enable operator = to work with all the permutations of your inherited classes, then using dynamic_cast<> and static_cast<> will do the trick, however - keep in mind that if you happen to need to use casts, you might wish to rethink your design.如果您真的希望operator =能够处理继承类的所有排列,那么使用dynamic_cast<>static_cast<>就可以解决问题,但是 - 请记住,如果您碰巧需要使用强制转换,您可能会希望重新考虑您的设计。 Their usage is not recommended .不推荐使用它们。

For a working example with ...cast usage, see here:有关...cast用法的工作示例,请参见此处:

class Base{
public:
    // mandatory for destructing the objects via pointer of a base class
    virtual ~Base() = default;

    virtual Base& operator = (const Base& other) {};
};

class VarInt : public Base{
public:
    virtual Base& operator = (const Base& other) override
    {
        const auto ptr = dynamic_cast<const VarInt*>(&other);
        if(ptr != nullptr){
            // do whatever you need to - the *other* object is of a type VarInt!
            // use *ptr* to use *other* as VarInt
            return *this;
        } else {
            // do whatever you need to, when *other* is NOT a VarInt!
            // throw an exception? Add nothing?
        }
    }
};

Please keep in mind that this is not recommended for everyday usage - casts are a sign of a design flaw and should not be preferred请记住,不建议将其用于日常使用 - 强制转换是设计缺陷的标志,不应首选

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

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