【问题标题】:How to print all variables in an object, and how to print a select set of variables?如何打印对象中的所有变量,以及如何打印一组选定的变量?
【发布时间】:2020-11-05 14:06:15
【问题描述】:
class Person
{
public:
        string fname;
        string lname;
        string occupation;
        string gender;
        int age;
};
int main()
{
        Person bc;
        bc.fname = "Bevelry";
        bc.lname = "Crusher";
        bc.gender = "female";
        bc.occupation = "Doctor, USS 1701-D";
        bc.age = 40;
        cout << bc.all << "\n"; //Something like this?
}

我是否可以在不自己指定的情况下打印对象的每个变量?我是否可以制作一个变量的选择列表,比如变量数组,然后打印它们?

编辑:我不小心把 cout 放在课堂上,现在修好了

【问题讨论】:

  • 不,C++(还)没有反射
  • 我怀疑这是meta.stackexchange.com/questions/66377/what-is-the-xy-problem 我想不出在没有任何表示细节的情况下应该输出对象的所有属性的情况。除了调试目的之外,即在希望查看所有值的元级别上。另一方面,对于调试,我建议使用调试器。因此,请提供有关您想要实现的更抽象级别的详细信息。因为即使我的猜测是错误的,对你的程序设计进行更大的更改可能会得到你想要的,而不是按照你想象的方式去做。
  • @Yunnosch 我没有什么具体的目标,我只是想知道我能不能做到。
  • 如果是这样,UnholySheep 的评论就是你的答案。 (但我对此表示怀疑。)
  • @0b11001001 您的问题是否得到以下答案的回答?如果是,请接受其中之一。

标签: c++ class object variables


【解决方案1】:

这是不可能的。在 C++ 中,没有任何东西可以满足您的需求。

【讨论】:

    【解决方案2】:

    一方面可以轻松地说这在 C++ 中是不可能的。

    然而,另一方面,人们可以补充一点,当人们在编写面向对象的计算机程序时,这种情况经常发生,而在 Java 中,the toString() method 就是为此而生的。因此,您实际上可以编写自己的 toString() 方法(基于它在 Java 中的完成方式),并使用它。

    祝你好运

    【讨论】:

      【解决方案3】:

      我是否可以在不自己指定的情况下打印对象的每个变量?

      没有。

      我是否可以选择变量列表,比如变量数组,然后打印它们?

      不是自动的,但您可以创建std::anys 的集合并自己添加一些解码。

      例子:

      #include <any>
      #include <functional>
      #include <iostream>
      #include <iterator>
      #include <string>
      #include <vector>
      
      // decode and print one std::any
      void decode_one(std::ostream& os, const std::any& a) {
          if(auto s = std::any_cast<std::reference_wrapper<std::string>>(&a)) os << s->get();
          else if(auto i = std::any_cast<std::reference_wrapper<int>>(&a)) os << *i;
          else if(auto d = std::any_cast<std::reference_wrapper<double>>(&a)) os << *d;
          // add more types to decode here
          else os << "<unknown type>";
      }
      
      // a custom ostream operator to decode a vector of anys:
      std::ostream& operator<<(std::ostream& os, const std::vector<std::any>& va) {
          auto begin = va.begin();
          auto end = va.end();
          if(begin != end) {
              decode_one(os, *begin);
              for(std::advance(begin, 1); begin != end; std::advance(begin, 1)) {
                  os << ',';
                  decode_one(os, *begin);
              }
          }
          return os;
      }
      
      int main() {
          int a = 10;
          std::string b = "Hello world";
          double c = 3.14159;
      
          std::vector<std::any> va{
              std::ref(a),
              std::ref(b),
              std::ref(c)
          };
          
          c *= 2.;                 // just to show that the vector holds a reference
      
          std::cout << va << '\n'; // print the variables in the vector
      }
      

      输出:

      10,Hello world,6.28318
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-08
        • 2018-06-03
        • 2022-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多