简体   繁体   English

如何重载从c ++中继承模板的类的赋值运算符

[英]How to overload the assignment operator for a class that inherits from a template in c++

I have a given utility template class tagged. 我有一个给定的实用程序模板类标记。 I had to declare 2 new structs using these template classes as follows. 我必须使用这些模板类声明2个新结构,如下所示。

tagged.h tagged.h

#ifndef TAGGED_H
#define TAGGED_H

#include <iostream>


template<typename T, typename TAG>
class tagged
{
private:
    T _value;

public:
    tagged() : _value() { }

    explicit tagged(const T& value) : _value(value) { }    

    // https://isocpp.org/wiki/faq/templates#template-friends
    friend T& value(tagged<T, TAG>& st)
    {
        return st._value;
    }

    friend const T& value(const tagged<T, TAG>& st)
    {
        return st._value;
    }
};

template<typename T>
struct equality
{
    friend bool operator ==(const T& x, const T& y)
    {
        return value(x) == value(y);
    }

    friend bool operator !=(const T& x, const T& y)
    {
        return value(x) != value(y);
    }
};

template<typename T>
struct ordered : equality<T>
{
    friend bool operator <(const T& x, const T& y)
    {
        return value(x) < value(y);
    }

    friend bool operator <=(const T& x, const T& y)
    {
        return value(x) <= value(y);
    }

    friend bool operator >(const T& x, const T& y)
    {
        return value(x) > value(y);
    }

    friend bool operator >=(const T& x, const T& y)
    {
        return value(x) >= value(y);
    }
};

These are the two structs i declared following the rules given by the assignment. 这是我按照作业规则宣布的两种结构。 primitives.h primitives.h

//Time
struct __declspec(empty_bases)Time : tagged<uint64_t, Time>, ordered<Time>, show_value<Time, int>{ using tagged::tagged; };


//Duration
struct __declspec(empty_bases)Duration : tagged<uint64_t, Duration>, ordered<Duration>, show_value<Duration, int> { using tagged::tagged; };

I succeeded in writing all other operators like + and - but i cant seem to solve how to overload += and -= I'm not allowed to change the objects in tagged.h I know assignment operators can only be member functions. 我成功编写了所有其他运算符,如+和 - 但我似乎无法解决如何重载+ =和 - =我不允许更改tagged.h中的对象我知道赋值运算符只能是成员函数。 Because of the way the template works i've tried casting 'const Time&' and const Duration& to non consts but that didnt seem to work. 由于模板的工作方式,我已经尝试了'const Time&'和const Duration和非consts,但这似乎没有用。 I've tried the examples you can find online about assigment operator overloading but the examples all overload in the template and not in the inherited class where I barely have write access to '_value' which is the value I should overwrite of reassign the pointer of. 我已经尝试了你可以在网上找到的关于assigment运算符重载的例子,但是这些例子都在模板中重载而不是在继承的类中,我几乎没有对'_value'的写访问权,这是我应该覆盖的重新分配指针的值。

Thanks 谢谢

edit: 编辑:

 struct __declspec(empty_bases)Time : tagged<uint64_t, Time>, ordered<Time>, show_value<Time, int>
    {
        using tagged::tagged;

        Time& operator+(const Duration& right) {
            Time t = Time(value(*this) + value(right));
            return t;
        };

        Time& operator+=(const Duration& right) {
            (uint64_t&)(*this) = value(*this) + value(right);
            return (*this);
        };

    };


    //Duration
    struct __declspec(empty_bases)Duration : tagged<uint64_t, Duration>, ordered<Duration>, show_value<Duration, int> {
        using tagged::tagged;

        Duration& operator+(const Duration& right) {
            Duration d = Duration(value(*this) + value(right));
            return d;
        };

        Time& operator+(const Time & right) {
            Time t = Time(value(*this) + value(right));
            return t;
        };

        Duration& operator-(const Time & right) {
            Duration d = Duration(value(*this) - value(right));
            return d;
        };

        Duration& operator-(const Duration & right) {
            Duration d = Duration(value(*this) - value(right));
            return d;
        };

         Duration& operator+=(const Duration& right) {
             (uint64_t&)(*this) = (uint64_t&)(*this) + (uint64_t&)(right);
             return (*this);
        }

         Duration& operator-=(const Duration& right) {
            (uint64_t&)(*this) = value(*this) - value(right);
            return (*this);
                };
            };

This is what I have now. 这就是我现在拥有的。 Still have the same syntax errors that keep popping up. 仍然有相同的语法错误,不断弹出。 I dont know anymore lmao 我不知道lmao

From what I see you should be able to implement it using value() function; 从我看到你应该能够使用value()函数实现它; Since value returns by reference something like this should work: 由于值通过引用返回类似这样的应该工作:

Duration & operator+=(const Duration & right) {
    value(*this) += value( right );
    return *this;
}

Just be careful on the other operators (I'm looking at + and -) because you return references to temporal objects. 请注意其他运算符(我正在查看+和 - ),因为您返回对临时对象的引用。

Duration & operator+(const Duration & right) {                // this returns a reference to an object
  Duration d = Duration( value( *this ) + value( right ) );   // this creates a temporal variable
  return d;                                                   // you return a reference to d bu its lifetime is over -> undefined behavior I believe
}

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

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