首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >算术和赋值运算符重载-返回值、范围、组合表达式

算术和赋值运算符重载-返回值、范围、组合表达式
EN

Stack Overflow用户
提问于 2013-02-27 20:38:22
回答 1查看 507关注 0票数 0

到目前为止,我的代码如下:

代码语言:javascript
复制
#include <iostream>
#include <vector>

using namespace std;

class Dictionary
{
private:
    string dictName;
    struct wordCard
    {
        string word;
        string translation;
    };
    vector<wordCard> Dict;
    bool foundit = false;
public:
    // My attemtp at swap function for copy-and-swap:
    void swap(Dictionary& dict1, Dictionary& dict2)
    {
        Dictionary dict3("tmp");
        dict3.dictName = dict1.dictName;
        dict3.Dict = dict1.Dict;
        dict1.dictName = dict2.dictName;
        dict1.Dict = dict2.Dict;
        dict2.dictName = dict3.dictName;
        dict2.Dict = dict3.Dict;
    }
    // Very basic constructor (setting the dictionary name while creating an object was part of the assignment):
    Dictionary(string name)
    {
        setDictName(name);
    }

    /* various functions that work fine */

    // Overloading "+" operator:
    // The result is supposed to be a new dictionary (without changing the source) where all words from the
    // original dictionaries are present without doubles.
    Dictionary& operator+ (const Dictionary& dict)
    {
        bool doubleword = false;
        string plusname;
        plusname = "Augmenting " + this->dictName + " & " + dict.dictName;
        Dictionary plusDict(plusname);
        plusDict.Dict = this->Dict;
        for (int i = 0; i < dict.Dict.size(); i++)
        {
            doubleword = false;
            for (int i2 = 0; i2 < plusDict.Dict.size(); i2++)
            {
                if (plusDict.Dict[i2].word == dict.Dict[i].word)
                {
                    doubleword = true;
                }
            }
            if (!doubleword)
            {
                plusDict.Dict.push_back(dict.Dict[i]);
            }
        }
        return *this;
    }

    /* 2 other overloads that are very similar */

    // Overloading "=" operator (using copy-and-swap):
    // Not part of the assignment, but I couldn't think of another way to make the other operators work.
    Dictionary& operator=(Dictionary dict)
    {
        swap(*this, dict);
        return *this;
    }
};

以及我对它的问题:

理想情况下,它应该是这样工作的:

代码语言:javascript
复制
Obj1 = result of operation Obj2 + Obj3;

我现在得到的是:

代码语言:javascript
复制
Obj1 = Obj2 (ignores Obj3)

我有一个模糊的想法(或者,实际上,有两个想法)。首先,operator+返回*this,而不是实际的结果。但当我试图将其更改为temp类对象时,编译器开始对我大喊大叫。其次,我知道我使用的是一个局部变量(temp类对象),但我不知道如何将其设置为公共变量,以便稍后使用。当我尝试向public:部分(或private:)添加类对象时,编译器将其视为函数声明,而不是类对象。

那么,如何使我的temp类对象成为公共对象,或者返回a+b而不是*this的结果,或者让operator=捕获result或operator+而不是它返回的内容?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-27 20:40:35

operator +应该通过值返回一个新对象,并且是常量-即类似于

代码语言:javascript
复制
Dictionary operator+ (const Dictionary& dict) const
{
    Dictionary ret;
    //modify ret
    return ret;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15112350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档