简体   繁体   English

C ++中自定义字符串类的<<运算符函数的返回值

[英]Return value for a << operator function of a custom string class in C++

I am trying to create my own std::string wrapper to extend its functionality. 我正在尝试创建自己的std :: string包装器来扩展其功能。 But I got a problem when declaring the << operator. 但是在声明<<运算符时遇到了问题。 Here's my code so far: 到目前为止,这是我的代码:

my custom string class: 我的自定义字符串类:

class MyCustomString : private std::string
{
public:
  std::string data;
  MyCustomString() { data.assign(""); }
  MyCustomString(char *value) { data.assign(value); }
  void Assign(char *value) { data.assign(value); }
  // ...other useful functions
  std::string & operator << (const MyCustomString &src) { return this->data; }
};

the main program: 主程序:

int main()
{
  MyCustomString mystring("Hello");
  std::cout << mystring; // error C2243: 'type cast' : conversion from 'MyCustomString *' to 'const std::basic_string<_Elem,_Traits,_Ax> &' exists, but is inaccessible

  return 0;
}

I wanted cout to treat the class as a std::string, so that I won't need to do something like: 我希望cout将该类视为std :: string,因此我不需要执行以下操作:

std::cout << mystring.data;

Any kind of help would be appreciated! 任何形式的帮助将不胜感激!

Thanks. 谢谢。

Just fyi: my IDE is Microsoft Visual C++ 2008 Express Edition. 只是fyi:我的IDE是Microsoft Visual C ++ 2008 Express Edition。

If you look at how all stream operators are declared they are of the form: 如果你看看如何声明所有流操作符,它们的形式如下:

ostream& operator<<(ostream& out, const someType& val );

Essentially you want your overloaded function to actually do the output operation and then return the new updated stream operator. 基本上,您希望重载函数实际执行输出操作,然后返回新更新的流操作符。 What I would suggest doing is the following, note that this is a global function, not a member of your class: 我建议做的是以下内容,请注意这是一个全局函数,而不是您的类的成员:

ostream& operator<< (ostream& out, const MyCustomString& str )
{
    return out << str.data;
}

Note that if your 'data' object was private, which basic OOP says it probably should, you can declare the above operator internally as a 'friend' function. 请注意,如果您的“数据”对象是私有的,可能应该使用哪个基本OOP,则可以在内部将上述运算符声明为“朋友”函数。 This will allow it to access the private data variable. 这将允许它访问私有数据变量。

You need a free-standing function (friend of your class, if you make your data private as you probably should!) 你需要一个独立的功能(你班上的朋友,如果你把你的data设为私有你可能应该!)

inline std::ostream & operator<<(std::ostream &o, const MyCustomString&& d)
{
    return o << d.data;
}

That's not how you overload the << operator. 这不是你重载<<运算符的方式。 You need to pass in a reference to an ostream and return one (so you can stack multiple <<, like std::cout << lol << lol2 ). 你需要传入一个对ostream的引用并返回一个(所以你可以堆叠多个<<,就像std::cout << lol << lol2 )。

ostream& operator << (ostream& os, const MyCustomString& s);

Then just do this: 然后就这样做:

ostream& operator << (ostream& os, const MyCustomString& s)
{
   return os << s.data;
}

Firstly, you seem to have an issue with the definition of MyCustomString. 首先,您似乎遇到了MyCustomString定义的问题。 It inherits privately from std::string as well as containing an instance of std::string itself. 它从std::string私下继承,并包含std::string本身的实例。 I'd remove one or the other. 我会删除其中一个。

Assuming you are implementing a new string class and you want to be able to output it using std::cout , you'll need a cast operator to return the string data which std::cout expects: 假设您正在实现一个新的字符串类,并且希望能够使用std::cout输出它,您需要一个强制转换操作符来返回std::cout期望的字符串数据:

operator const char *()
{
    return this->data.c_str();
}

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

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