Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.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++_Class_Object - Fatal编程技术网

C++ 列出C+的成员+;班

C++ 列出C+的成员+;班,c++,class,object,C++,Class,Object,假设我有一门课,如下所示 class Rectangle{ public: int height; int width; }; 我如何打印出这个类成员的列表,而不必手动地说“代码> cOUT。看来你要重载运算符看起来好像要重载运算符,正如其他人指出的,C++没有提供任何方法来自动完成这个操作。< /P> P> > C++中的良好编码实践是提供一个类及其成员的声明,并尽可能地注释和记录在一个与在声明的类中相同的头文件。正如其他人指出的,C++没有提供自动完成这一操作的

假设我有一门课,如下所示

class Rectangle{
    public:
    int height;
    int width;

};

我如何打印出这个类成员的列表,而不必手动地说“代码> cOUT

。看来你要重载运算符看起来好像要重载运算符,正如其他人指出的,C++没有提供任何方法来自动完成这个操作。< /P>


<> P> > C++中的良好编码实践是提供一个类及其成员的声明,并尽可能地注释和记录在一个与在声明的类中相同的头文件。

正如其他人指出的,C++没有提供自动完成这一操作的方法。


<> P> C++中的良好编码实践是提供一个类及其成员的声明,并尽可能地注释和记录在一个与内部声明的类相同的头文件中。

C++没有任何内置的内省功能。你能告诉我们更多关于你想要达到的目标和原因吗?你不可能那么容易做到。我想你的一些朋友一定告诉过你,你可以用Java做到这一点。。(正确的…java中有反射API),但是在C++中没有这样的功能。您可以阅读前面的文章,这是一个尝试,如果您想生成一个类成员的文档,可以使用doXEGEN(看起来像JavaDoc)。C++没有任何内置的内省功能。你能告诉我们更多关于你想要达到的目标和原因吗?你不可能那么容易做到。我想你的一些朋友一定告诉过你,你可以用Java做到这一点。。(正确的…java中有反射API),但是在C++中没有这样的功能。您可以阅读前一篇文章,这是一次尝试。如果您想生成关于类成员的文档,您可以使用Doxygen(看起来有点像Javadoc)。感谢您的帮助,但是如果其他人加入了一个新类并希望在不重载@sanke93的情况下打印出成员列表,那么这就不起作用了这就是为什么你会重载对它的感谢,但如果其他人加入了一个新类,并希望在不重载@sanke93的情况下打印出成员列表,那么这就不起作用了,这就是为什么要重载
Rectangle rect;
std::cout << rect;
Rectangle rect;
std::cout << "Width: " << rect.width << '\n';
std::cout << "Height: " << rect.width;
std::ostream& operator<<(std::ostream&, const Type& type);
std::cout << "Hello " << " operator<< is returning me " << " cout so I " << " can continue to do this\n";
class Rectangle{
  public:
    int height;
    int width;
};

// the following usually goes in an implementation file (i.e. .cpp file), 
// with a prototype in a header file, as any other function
std::ostream& operator<<(std::ostream& output, const Rectangle& rect) 
{
    return output << "width: " << rect.width <<< "\nheight: " << rect.height;
}
class Rectangle{
  public:
    int height;
    int width;

    // friend function
    friend std::ostream& operator<<(std::ostream& output, const Rectangle& rect);
};


std::ostream& operator<<(std::ostream& output, const Rectangle& rect)
{
    return output << "width: " << rect.width <<< " height: " << rect.height;
}