简体   繁体   English

C ++模板类运算符重载

[英]C++ template class operator overloading

So I'm trying to build a template class for a study project about group theory and logic. 因此,我正在尝试为有关小组理论和逻辑的研究项目构建模板类。 I have the class: 我上课:

#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
template <class T>
class CSet
{
private:
    T* arr;
    int size;
protected:

public:
    /*Constructors & Destructor*/
    CSet(int s = 0, T* a = NULL); //Default constructor
    CSet(const CSet<T> & obj_input); //Copy constructor
    ~CSet() { delete[] arr; } //Destructor

    /*Operators Overloading*/
    const CSet<T>& operator=(const CSet<T>& obj_input); // = overloading
    const CSet<T>& operator+=(const T& val_input); // += overloading
    const CSet<T>& operator-=(const T& val_input); // -= overloading

    /*Logic Operators Overloading*/
    const CSet<T>& operator|(const CSet<T>& obj_input);
    const CSet<T>& operator&(const CSet<T>& obj_input);
    const CSet<T>& operator-(const CSet<T>& obj_input);
    bool operator==(const CSet<T>& obj_input);
    bool operator!=(const T& val_input);
    bool operator>(const CSet<T>& obj_input);
    const CSet<T>& operator^(const CSet<T>& obj_input);

    //void DifWrite(const CSet<T>& obj_input); //does - and outputs to file

    friend ostream& operator<<(ostream& op, const CSet<T>& input) {
        for (int i = 0; i < input.size; i++)
        {
            op << input.arr[i] << " ";
        }
        return op;
    }
};

and I'm trying to make the | 而我正在努力使| operator to simulate OR logic function. 运算符以模拟OR逻辑功能。 This means that if I make A={1,2,3} and B={3,4,5} then A|B={1,2,3,4,5} which is a new object. 这意味着如果我使A = {1,2,3}和B = {3,4,5},则A | B = {1,2,3,4,5}是一个新对象。 However I can't decipher how to allocate memory for the new object and return it. 但是我无法破译如何为新对象分配内存并返回它。 The function I currently have is changing 'this' instead of returning a new object: 我目前拥有的函数正在更改“ this”,而不是返回新对象:

template <class T>
const CSet<T>& CSet<T>::operator|(const CSet<T>& obj_input) {
    if (!arr)   
    {
        *this = obj_input;
        return *this;
    }
    else if (!obj_input.arr)
    {
        return *this;
    }
    else
    {
        for (int i = 0; i < size; i++)
        {
            temp += this->arr[i];
        }
        for (int i = 0; i < obj_input.size; i++)
        {
            temp += obj_input.arr[i];
        }
        *this = temp;
    }
    return *this;
}

You don't want to return a constant reference to an object you are creating in the function. 您不想返回对在函数中创建的对象的常量引用。 What you should do is create a object in the function and then return it by value. 您应该做的是在函数中创建一个对象,然后按值返回它。 To do that your code would be: 为此,您的代码将是:

template <class T>
CSet<T> CSet<T>::operator|(const CSet<T>& obj_input) const
{
    CSet<T> temp;
    temp.size = *this.size + obj_input.size;
    temp.arr = new T[temp.size];
    int i = 0;
    for (; i < *this.size; i++)
        temp.arr[i] = *this.arr[i];
    for (; i - *this.size < obj_input.size; i++)
        temp.arr[i] = *this.arr[i];
    return temp;
}

If you instead use a std::vector instead of raw arrays your function would become: 如果改用std::vector而不是原始数组,则函数将变为:

template <class T>
CSet<T> CSet<T>::operator|(const CSet<T>& obj_input) const
{
    CSet<T> temp;
    temp.arr.insert(temp.arr.end(), *this.arr.begin(), *this.arr.end());
    temp.arr.insert(temp.arr.end(), obj_input.arr.begin(), obj_input.arr.end())
    return temp;
}

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

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