• <tfoot id='H6YN3'></tfoot>

    <i id='H6YN3'><tr id='H6YN3'><dt id='H6YN3'><q id='H6YN3'><span id='H6YN3'><b id='H6YN3'><form id='H6YN3'><ins id='H6YN3'></ins><ul id='H6YN3'></ul><sub id='H6YN3'></sub></form><legend id='H6YN3'></legend><bdo id='H6YN3'><pre id='H6YN3'><center id='H6YN3'></center></pre></bdo></b><th id='H6YN3'></th></span></q></dt></tr></i><div id='H6YN3'><tfoot id='H6YN3'></tfoot><dl id='H6YN3'><fieldset id='H6YN3'></fieldset></dl></div>
  • <legend id='H6YN3'><style id='H6YN3'><dir id='H6YN3'><q id='H6YN3'></q></dir></style></legend>

      <bdo id='H6YN3'></bdo><ul id='H6YN3'></ul>
  • <small id='H6YN3'></small><noframes id='H6YN3'>

        如何在一个列表中存储不同的数据类型?(C++)

        时间:2023-10-07

        • <small id='4oPw7'></small><noframes id='4oPw7'>

        • <i id='4oPw7'><tr id='4oPw7'><dt id='4oPw7'><q id='4oPw7'><span id='4oPw7'><b id='4oPw7'><form id='4oPw7'><ins id='4oPw7'></ins><ul id='4oPw7'></ul><sub id='4oPw7'></sub></form><legend id='4oPw7'></legend><bdo id='4oPw7'><pre id='4oPw7'><center id='4oPw7'></center></pre></bdo></b><th id='4oPw7'></th></span></q></dt></tr></i><div id='4oPw7'><tfoot id='4oPw7'></tfoot><dl id='4oPw7'><fieldset id='4oPw7'></fieldset></dl></div>

              • <bdo id='4oPw7'></bdo><ul id='4oPw7'></ul>

                  <tbody id='4oPw7'></tbody>
                <legend id='4oPw7'><style id='4oPw7'><dir id='4oPw7'><q id='4oPw7'></q></dir></style></legend>

                1. <tfoot id='4oPw7'></tfoot>
                  本文介绍了如何在一个列表中存储不同的数据类型?(C++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要存储一个对象的各种属性的列表.属性由名称和数据组成,可以是任何数据类型.

                  I need to store a list of various properties of an object. Property consists of a name and data, which can be of any datatype.

                  我知道我可以创建一个Property"类,并使用不同的 PropertySubClass 对其进行扩展,这些 PropertySubClass 仅因它们存储的数据类型而异,但感觉不对.

                  I know I can make a class "Property", and extend it with different PropertySubClasses which only differ with the datatype they are storing, but it does not feel right.

                  class Property
                  {
                       Property(std::string name);
                       virtual ~Property();
                  
                       std::string m_name;
                  };
                  
                  class PropertyBoolean : Property
                  {
                       PropertyBoolean(std::string name, bool data);
                  
                       bool m_data;
                  };
                  
                  class PropertyFloat : Property
                  {
                       PropertyFloat(std::string name, float data);
                  
                       float m_data;
                  };
                  
                  class PropertyVector : Property
                  {
                       PropertyVector(std::string name, std::vector<float> data);
                  
                       std::vector<float> m_data;
                  };
                  

                  现在我可以将各种属性存储在一个

                  Now I can store all kinds of properties in a

                   std::vector<Property*>
                  

                  为了获取数据,我可以将对象转换为子类.或者我可以创建一个纯虚函数来处理函数内部的数据,而无需进行强制转换.

                  and to get the data, I can cast the object to the subclass. Or I can make a pure virtual function to do something with the data inside the function without the need of casting.

                  无论如何,创建这些不同类型的子类的感觉并不正确,这些子类仅因它们存储的数据类型而异.有没有其他方便的方法来实现类似的行为?

                  Anyways, this does not feel right to create these different kind of subclasses which only differ by the data type they are storing. Is there any other convenient way to achieve similar behavior?

                  我无法访问 Boost.

                  I do not have access to Boost.

                  推荐答案

                  C++ 是一种多范式语言.在混合范式的地方,它最闪耀,最强大.

                  C++ is a multi-paradigm language. It shines brightest and is most powerful where paradigms are mixed.

                  class Property
                  {
                  public:
                      Property(const std::string& name) //note: we don't lightly copy strings in C++
                        : m_name(name) {}
                      virtual ~Property() {}
                  private:
                      std::string m_name;
                  };
                  
                  template< typename T >
                  class TypedProperty : public Property
                  {
                  public:
                      TypedProperty (const std::string& name, const T& data)
                        : Property(name), m_data(data);
                  private:
                      T m_data;
                  };
                  
                  typedef std::vector< std::shared_ptr<Property> > property_list_type;
                  

                  <小时>

                  为什么使用 std::shared_ptr 而不是 Property*?
                  考虑这个代码:


                  Why using std::shared_ptr<Property> instead of Property*?
                  Consider this code:

                  void f()
                  {
                    std::vector<Property*> my_property_list;
                    for(unsigned int u=0; u<10; ++u)
                      my_property_list.push_back(new Property(u));
                  
                    use_property_list(my_property_list);
                  
                    for(std::vector<Property*>::iterator it=my_property_list.begin();
                                                        it!=my_property_list.end(); ++it)
                      delete *it;
                  }
                  

                  那个 for 循环试图清理,删除向量中的所有属性,就在它超出范围并带走所有指针之前.
                  现在,虽然这对于新手来说可能看起来不错,但如果您只是一位经验丰富的 C++ 开发人员,那么一旦您看到该代码,就会引起警钟.

                  That for loop there attempts to cleanup, deleting all the properties in the vector, just before it goes out of scope and takes all the pointers with it.
                  Now, while this might seem fine for a novice, if you're an only mildly experienced C++ developer, that code should raise alarm bells as soon as you look at it.

                  问题是对 use_property_list() 的调用可能会引发异常.如果是这样,函数 f() 将立即离开.为了正确清理,将调用 f() 中创建的所有自动对象的析构函数.也就是说,my_property_list 将被正确销毁.std::vector 的析构函数会很好地清理它保存的数据.然而,它持有指针std::vector如何知道这些指针是否是最后一个引用它们的对象?
                  由于它不知道,它不会删除对象,它只会在销毁其内容时销毁指针,从而在堆上留下您不再有任何指针指向的对象.这就是所谓的泄漏".

                  The problem is that the call to use_property_list() might throw an exception. If so, the function f() will be left right away. In order to properly cleanup, the destructors for all automatic objects created in f() will be called. That is, my_property_list will be properly destroyed. std::vector's destructor will then nicely cleanup the data it holds. However, it holds pointers, and how should std::vector know whether these pointers are the last ones referencing their objects?
                  Since it doesn't know, it won't delete the objects, it will only destroy the pointers when it destroys its content, leaving you with objects on the heap that you don't have any pointers to anymore. This is what's called a "leak".

                  为了避免这种情况,您需要捕获所有异常,清理属性,然后重新抛出异常.但是,十年后,有人必须为 10MLoC 应用程序添加一个新功能,这已经发展到,并且匆忙添加了在某些条件成立时过早地离开该功能的代码.代码经过测试,可以正常工作并且不会崩溃 - 只有它所在的服务器现在每小时泄漏几个字节,导致它由于大约每周一次内存不足而崩溃.发现这使得精细调试很多小时.

                  In order to avoid that, you would need to catch all exceptions, clean up the properties, and the rethrow the exception. But then, ten years from now, someone has to add a new feature to the 10MLoC application this has grown to, and, being in a hurry, adds code which leaves that function prematurely when some condition holds. The code is tested and it works and doesn't crash - only the server it's part of now leaks a few bytes an hour, making it crash due to being out of memory about once a week. Finding that makes for many hours of fine debugging.

                  底线:永远不要手动管理资源,始终将它们包装在旨在处理此类资源的一个实例的类的对象中.对于动态分配的对象,这些句柄称为智能指针",最常用的是shared_ptr.

                  Bottom line: Never manage resources manually, always wrap them in objects of a class designed to handle exactly one instance of such a resource. For dynamically allocated objects, those handles are called "smart pointer", and the most used one is shared_ptr.

                  这篇关于如何在一个列表中存储不同的数据类型?(C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么我可以在 C++ 中的函数中定义结构和类? 下一篇:双向地图是否有更有效的实现?

                  相关文章

                  最新文章

                2. <i id='gcutm'><tr id='gcutm'><dt id='gcutm'><q id='gcutm'><span id='gcutm'><b id='gcutm'><form id='gcutm'><ins id='gcutm'></ins><ul id='gcutm'></ul><sub id='gcutm'></sub></form><legend id='gcutm'></legend><bdo id='gcutm'><pre id='gcutm'><center id='gcutm'></center></pre></bdo></b><th id='gcutm'></th></span></q></dt></tr></i><div id='gcutm'><tfoot id='gcutm'></tfoot><dl id='gcutm'><fieldset id='gcutm'></fieldset></dl></div>

                    <legend id='gcutm'><style id='gcutm'><dir id='gcutm'><q id='gcutm'></q></dir></style></legend>
                  1. <small id='gcutm'></small><noframes id='gcutm'>

                  2. <tfoot id='gcutm'></tfoot>

                      • <bdo id='gcutm'></bdo><ul id='gcutm'></ul>