reverse 函数不会反转 class 实例化的引用字符串

reverse function doesn't reverse the referenced string of a class instantiation

我正在尝试反转 FunnyNumber class 中的字符串。问题是,当我在 f2 的 main 中调用 reverse 方法时,它不会反转 f2 字符串。但是当我从反向方法实现中打印出反向字符串时,它就起作用了。此外,我还重载了 << 运算符以打印出 class 数字中的字符串值,该数字是在 FunnyNumber class 中继承的。任何帮助,将不胜感激。

#ifndef NUMBER_H
#define NUMBER_H

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

class Number {
public:
  // Make a number with value 0
  Number();
  // Make a number with value val
  Number(string val);

  // Get the number's value
  virtual string getValue() const;

  // Print this number to the stream
  virtual void print(ostream& stream) const;

  // Read this number from the stream
  virtual void read(istream& stream);

  // Overload the insertion operator
  friend ostream& operator <<(ostream& outs, const Number& n);

  // Overload the extraction operator
  friend istream& operator >> (istream& ins, Number& n);

protected:
  string value;
};

#endif 


Number::Number()
{
  value = "";
}
Number::Number(string args)
{
  value = args;
}
string Number::getValue()const
{
  return value;
}
ostream& operator <<(ostream& outs, const Number& n)
{
  n.print(outs);
  return outs;
}
void Number::print(ostream& stream)const
{
  stream << getValue();
}
void Number::read(istream& stream)
{
  stream >> value;
}
istream& operator >> (istream& ins, Number& n)
{
  n.read(ins);
  return ins;
}

#ifndef FUNNYNUMBER_H
#define FUNNYNUMBER_H

#include<iostream>
#include<string>
#include"Number.h"
#include<algorithm>


using namespace std;

class FunnyNumber : public Number 
{
public:
    FunnyNumber();
    FunnyNumber(string val);
    virtual string operator+(const FunnyNumber &other)const;
    virtual bool operator==(const FunnyNumber &other)const;
    void reverse();
    int find_first_not_this(char a);

protected:
    string value;

};
#endif // !FUNNYNUMBERS_H

FunnyNumber::FunnyNumber()
{
    value = "";
}

FunnyNumber::FunnyNumber(string val) : Number(val)
{
    value = val;
}

string FunnyNumber::operator+ (const FunnyNumber& other)const
{
    return getValue() + other.getValue();
}
int FunnyNumber::find_first_not_this(char a)
{
    int pos = 0;

    for(int i = 0; i < value.length(); i++)
    {
        if(value[i] != a)
        {
            pos = i;
            return pos;
        }
    }
    return pos;
}
bool FunnyNumber::operator==(const FunnyNumber& other)const
{
    bool isEqual = true;
    for (int i = 0; i < other.getValue().length(); i++) 
    {
        bool found = false;
        for (int j = 0; j < getValue().length(); j++) 
        {
            if(getValue()[j] == other.getValue()[i])
            {
                found = true;
                break;
            }
        }
        if(!found)
        {
            isEqual = found;
            return isEqual;
        }
    }
    return isEqual;
}

void FunnyNumber::reverse()
{
    std::reverse(value.begin(), value.end());
    value.erase(0, find_first_not_this('0'));
}


#include <iostream>
#include<string.h>
#include "FunnyNumber.h"

using namespace std;

int main()
{
   FunnyNumber f2;
   f2 = FunnyNumber("223");
   f2.reverse();
   cout<<"Reversed value "<<f2<<endl;

   system("pause");
   return 0;
}

输出是 223 而不是 322

重载运算符 << 是 Number class 上的友元函数,而友元函数不是 inherited

class Number {
public:
  // Make a number with value 0
  Number();
  // Make a number with value val
  Number(string &val);

  // Get the number's value
  virtual string getValue() const;

  // Print this number to the stream
  virtual void print(ostream& stream) const;

  // Read this number from the stream
  virtual void read(istream& stream);

  // Overload the insertion operator
  friend ostream& operator <<(ostream& outs, const Number& n);

  // Overload the extraction operator
  friend istream& operator >> (istream& ins, Number& n);

protected:
  string *value;
};
Number::Number()
{
    value = NULL;
}
Number::Number(string &args)
{
  value = &args;
}
string Number::getValue()const
{
  return *value;
}
ostream& operator <<(ostream& outs, const Number& n)
{
  n.print(outs);
  return outs;
}
void Number::print(ostream& stream)const
{
  stream << getValue();
}
void Number::read(istream& stream)
{
  stream >> *value;
}
istream& operator >> (istream& ins, Number& n)
{
  n.read(ins);
  return ins;
}

您的 FunnyNumber 将值存储两次,一次在类型 Number 的子对象中,一次在 string FunnyNumber::value.

您的 reverse 函数修改了第二个,但对 Number 基础子对象没有任何影响。然后,您调用的唯一输出函数正在 Number 基础子对象上工作,并且对 string FunnyNumber::value 一无所知。这就是为什么打印的不是反转的结果。