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

时间:2017-05-29 03:18:51

标签: c++ string inheritance reverse

我正在尝试从FunnyNumber类中反转字符串。问题是,当我在f2上调用main方法时,它不会反转f2字符串。但是当我从反向方法实现中打印出反向字符串时,它可以工作。我也重载了<<运算符,用于打印在FunnyNumber类中继承的类Number中的字符串值。任何帮助,将不胜感激。

#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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

重载运算符&lt;&lt;是Number类的友元函数,而友元函数不是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;
}