Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c++;格式化程序类? 我使用的是由第三方提供的C++类,因此不能修改它。它有许多属性,但没有方法或运算符重载(_C++ - Fatal编程技术网

c++;格式化程序类? 我使用的是由第三方提供的C++类,因此不能修改它。它有许多属性,但没有方法或运算符重载(

c++;格式化程序类? 我使用的是由第三方提供的C++类,因此不能修改它。它有许多属性,但没有方法或运算符重载(,c++,C++,是的。您可以将流插入运算符作为非成员函数重载。当然,缺点是您不能使该函数成为朋友(通常这样做),因此您将无法通过公共访问器输出未被类公开的任何内容——但无论您在无法修改类的情况下做什么,您都会受到这方面的限制 例如: class Foo { public: std::string name() const; int number() const; private: // Don't care about what's in here; can't access i

是的。您可以将流插入运算符作为非成员函数重载。当然,缺点是您不能使该函数成为朋友(通常这样做),因此您将无法通过公共访问器输出未被类公开的任何内容——但无论您在无法修改类的情况下做什么,您都会受到这方面的限制

例如:

class Foo {
  public:
    std::string name() const;
    int number() const;
  private:
    // Don't care about what's in here; can't access it anyway.
};

// You write this part:

std::ostream& operator<< (std::ostream& os, const Foo& foo) {
  // Format however you like in here, e.g.
  os << "(" << foo.name() << "," << foo.number() << ")";
  return os;
}

// Then you can write:

Foo foo;
std::out << foo;
class-Foo{
公众:
std::string name()常量;
int number()常量;
私人:
//不要关心这里有什么,反正也无法访问它。
};
//你写这部分:

std::ostream&operatorAn
operator@JamesMcNellis:它几乎肯定不适用于这种情况,但我可以想象一种病态的情况,即希望从一个不可修改类的私有成员中生成输出,而该类也无法通过其公共接口访问。