简体   繁体   English

C ++模板和运算符重载

[英]C++ Templates and operator overloading

I am learning templates and operator overloading. 我正在学习模板和运算符重载。 I have written some code but I am confused... Let me explain... 我已经写了一些代码,但是我很困惑...让我解释一下...

template <class T>
class tempType 
{
public:
    T value;
    bool locked;

    tempType():locked(false)
    { 
        value = 0; 
    }

    T operator=(T val)
    { 
        value=val;
        return value; 
    }
    tempType<T> operator=(tempType<T> &val)
    { 
        value=val.value; 
        return *this; 
    }
    operator T()
    { 
        return value; 
    }
};

And I did... 我做到了

int main(void)
{
    tempType<int> i;
    tempType<bool> b;
    tempType<float> f;
    i.value = 10;
    i = i + f;
    return 0;
}

What code I need to write in order to execute 我需要编写什么代码才能执行

tempType<T> operator=(tempType<T> &val){}

Also, I why operator T() is required? 另外,为什么需要operator T()

I think I know all the answers so I might as well post full response. 我想我知道所有答案,所以我不妨发表完整答复。

To override default operator= you should declare it as tempType<T> operator=(const tempType<T> &val){} . 要覆盖默认的operator=您应该将其声明为tempType<T> operator=(const tempType<T> &val){} Now you need to call the method explicitly via i.operator=(other_i). 现在,您需要通过i.operator=(other_i).显式调用该方法i.operator=(other_i).

If you correct the declaration you can use it like this: 如果您更正了声明,则可以这样使用它:

tempType<int> i;
tempType<int> other_i;
i = other_i; // this is what you just defined

The operator T() is called a conversion operator . operator T()称为转换运算符 It is kind of reverse or counter part of the conversion constructor which in your case would be tempType(const &T value) . 它是转换构造函数的反向部分或计数器部分,在您的情况下为tempType(const &T value)

It is used to convert a class object into a given type. 它用于将类对象转换为给定类型。 So in your case you would be able to write: 因此,根据您的情况,您可以编写:

tempType<int> i;
int some_int;
some_int = i; // tempType<int> gets converted into int via `operator int()`

Unless you implement move semantics, operator= should always take a const & reference to the source value. 除非您实现移动语义,否则operator=始终应使用const &引用源值。 It should also return a reference to the modified object. 它还应返回对修改后的对象的引用。

tempType & operator=(T const & val)
{ 
    value=val;
    return * this;
}

operator T is an implicit conversion function which allows any tempType object to be treated as an object of its underlying type T . operator T是一个隐式转换函数,该函数允许将任何tempType对象视为其基础类型T的对象。 Be careful when specifying implicit conversions that they won't conflict with each other. 指定隐式转换时,请注意它们不会相互冲突。

An implicit conversion function usually shouldn't make a copy, so you probably want 隐式转换函数通常不应复制,因此您可能想要

operator T & ()
{
    return value; 
}

operator T const & () const
{
    return value;
}

Given these, you shouldn't need another overload of operator = because the first overload will simply be adapted by the conversion function to a call such as i = b; 有了这些,您就不需要再重载operator =因为第一个重载将由转换函数简单地适应于诸如i = b;的调用i = b; .

If a series of conversions will result in the operator=(T const & val) being called, you should avoid also defining operator=(tempType const & val) because the overloads will compete on the basis of which conversion sequence is "better," which can result in a brittle (finicky) interface that may refuse to do seemingly reasonable things. 如果一系列转换将导致对operator=(T const & val)的调用,则应避免同时定义operator=(tempType const & val)因为重载将根据转换顺序“更好”进行竞争,这可能会导致界面脆弱(挑剔),从而可能拒绝执行看似合理的操作。

template <class T>
class tempType 
{
public:
    T value;
    bool locked;

    tempType() : value(), locked(false)
    { 
        value = 0; 
    }

    //althought legal, returning something different from tempType&
    //from an operator= is bad practice
    T operator=(T val)
    { 
        value=val;
        return value; 
    }
    tempType& operator=(const tempType &val)
    { 
        value=val.value; 
        return *this; 
    }
    operator T()
    { 
        return value; 
    }
};

int main(void)
{
    tempType<int> a;
    tempType<int> b;
    a = b;
    return 0;
}

in the code, a = b calls the operator. 在代码中, a = b调用运算符。

As for the second question, the operator T() is not needed "by default". 至于第二个问题,“默认情况下”不需要operator T() In your example, it is used when you write i+f : 在您的示例中,当您编写i+f时使用它:

  1. i is converted to an int i被转换为整数
  2. f is converted to a float f转换为浮点数
  3. the operation (+) is performed 执行操作(+)
  4. T tempType<int>::operator=(T val) is called for the assignement T tempType<int>::operator=(T val)进行分配

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

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