【问题标题】:Trouble with Operator Overloading and the *this Pointer运算符重载和 *this 指针的问题
【发布时间】:2014-03-09 01:43:31
【问题描述】:

我正在为自制的动态数组类进行运算符重载。我也在尝试学习如何使用 *this 指针,但进展并不顺利。以下是我认为需要解释该问题的课程部分和我的代码。

我不明白为什么当 *this 指针指向 + 等式的左侧时,我不能在 *this 指针上调用成员函数。

这里是调用 + 运算符的存根驱动程序:

> 已经超载并且正在工作。

cout << "Please enter a word to add:";
    string theWord;
    cin >> theWord;
    //add word
    array1 = array1 + theWord;
    cout << "array1: " << array1 << endl;

这里是主要代码:

class DynamicArray
{
public:
    //constructor
    DynamicArray(int initialcapacity = 10);
    //copy constructor
    DynamicArray(const DynamicArray& rhs);
    //destructor
    ~DynamicArray();

    //operator+ - add a string
    DynamicArray operator+(const string& rhs) const;

    //operator+ - concatenate another DynamicArray
    DynamicArray operator+(const DynamicArray& rhs) const;

    //change the capacity of the DynamicArray to the newCapacity - 
    //  may reduce the size of the array - entries past newCapacity will be lost
    void resize(int newCapacity);

private:
    string* mWords;//pointer to dynamic array of strings
    int mNumWords;//the current number of words being kept in the dynamic array
    int mCapacity;//the current capacity of the dynamic array (how many strings could fit in the array)

    //display all the contained strings (each on a newline) to the output stream provided
    void displayContents(ostream& output) const;

    //add all the strings contained in the input stream to the dynamic array - resize if necessary
    //return how many words are added to the array
    int addWords(ifstream &input);

    //add a single word to the dynamic array - resize if necessary
    void addWord(const string& word);

};

//add a single word to the dynamic array - resize if necessary
void DynamicArray::addWord(const string& word)
{
    if (mNumWords >= mCapacity)//need more space?
    {
        resize(mCapacity + 1);
    }

    mWords[mNumWords] = word;
    mNumWords++;
}

这是我目前正在开发的功能

//operator+ - add a string
DynamicArray DynamicArray::operator+(const string& rhs) const
{
    //this doesn't work, why doesn't it, how should/do I use the
    //this pointer properly
    this.addWord(rhs);

    return *this;
}

【问题讨论】:

  • 由于this 是一个指针,所以使用-&gt; 表示法,例如:this-&gt;addWord(rhs);
  • 使用 .而不是->。但这仍然行不通。看起来 Paul 下面所说的 addWord 不是 const 函数是问题所在。我必须自己编写而不是调用 addWord()
  • 不要“自己编写而不是调用 addWord()”。相反,花时间编写一个带字符串的运算符 +=。然后,您将“一石二鸟”。您现在将拥有 1) 一个有意义的运算符 +=,2) 您的运算符 + 成为一个简单的函数(请参阅下面的答案)。

标签: c++ arrays pointers operator-overloading this


【解决方案1】:

除了使用 this. 而不是 this->,您还定义了 operator+ 作为 const 成员函数。这意味着不能改变任何成员,并且不会执行对非常量成员函数的调用。您的 addword 函数是非常量的。这就是错误的原因——你违反了 const 正确性。

另外,你为什么只在代码中调用 operator + 来改变数组?该运算符不需要更改当前对象的任何方面。我可以理解 operator += 改变对象,但不是 operator +。

您应该首先编写一个运算符 +=。这个操作符可以改变当前对象,因为这是 += 的语义(改变当前对象)。那么运算符+可以这样写:

//operator+ - add a string
DynamicArray DynamicArray::operator+(const string& rhs) const
{
    DynamicArray temp = *this;
    temp += rhs;
    return temp;
}

这是假设你有一个工作拷贝构造函数。在上面的示例中,您正在改变一个临时对象,在该临时对象上调用 +=,然后返回该临时对象。 this 对象没有改变。

下面是您的运算符 += 的样子:

DynamicArray& DynamicArray::operator+=(const string& rhs) 
{
    this.addWord(rhs);
    return *this;
}

现在运算符 += 变为非常量,因为 += 的目的是更改当前对象。请注意,返回值是对当前对象的引用。这现在与上面的运算符 + 协同工作。

【讨论】:

  • 感谢您解释 const 部分。我不需要改变数组来添加一个单词吗?
  • “否”关于突变的问题。看看使用整数的简单加法。 a = b + c; 如果 a、b 和 c 是整数,那么在那个例子中 b 和 c 会改变吗?当然不是。对象的行为方式应该相同。
  • 我没有使用 std::string 的有效构造函数,我不需要 += 因为我将再次重载 + 运算符,但它会采用 DynamicArray 作为其参数。像这样的东西怎么样:if (mNumWords &lt; mCapacity)mWords[mNumWords] = rhs;
  • 查看我的更新答案。这就是工作运算符 += 所需要的一切。此外,如果我看到一个带有 operator + 的类,为什么我不能也有 operator +=?
  • 不会 += 将另一个 DynamicArray 作为参数,这意味着您不能传入字符串?
【解决方案2】:

您的operator+ 必须是非常量的,因为您想在每次调用它时更改对象的状态。你也不必写

this->addWord( rhs);  // correct but not needed  

因为在 this 指针上隐式调用了类成员函数内部的方法。鉴于此,你可以写:

DynamicArray DynamicArray::operator+(const string& rhs)
{
    addWord( rhs);
    return *this;
}

您也可以将此运算符实现为void

void DynamicArray::operator+(const string& rhs)
{
    addWord( rhs);
}

并以这种方式使用它:

array1 + theWord;

【讨论】:

    猜你喜欢
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多