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

      1. <tfoot id='Hx6aX'></tfoot>

        具有成员函数的 C++ 结构与具有公共变量的类

        C++ Structs with Member Functions vs. Classes with Public Variables(具有成员函数的 C++ 结构与具有公共变量的类)
      2. <legend id='OEtYq'><style id='OEtYq'><dir id='OEtYq'><q id='OEtYq'></q></dir></style></legend>
        1. <small id='OEtYq'></small><noframes id='OEtYq'>

              <tbody id='OEtYq'></tbody>
              • <bdo id='OEtYq'></bdo><ul id='OEtYq'></ul>

                1. <i id='OEtYq'><tr id='OEtYq'><dt id='OEtYq'><q id='OEtYq'><span id='OEtYq'><b id='OEtYq'><form id='OEtYq'><ins id='OEtYq'></ins><ul id='OEtYq'></ul><sub id='OEtYq'></sub></form><legend id='OEtYq'></legend><bdo id='OEtYq'><pre id='OEtYq'><center id='OEtYq'></center></pre></bdo></b><th id='OEtYq'></th></span></q></dt></tr></i><div id='OEtYq'><tfoot id='OEtYq'></tfoot><dl id='OEtYq'><fieldset id='OEtYq'></fieldset></dl></div>
                  <tfoot id='OEtYq'></tfoot>
                  本文介绍了具有成员函数的 C++ 结构与具有公共变量的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这确实是一个关于良好形式/最佳实践的问题.我在 C++ 中使用结构来形成旨在基本上保存数据的对象,而不是使用大量访问器方法创建一个类,这些访问器方法除了获取/设置值之外什么都不做.例如:

                  This is really a question of good form/best practices. I use structs in C++ to form objects that are designed to basically hold data, rather than making a class with a ton of accessor methods that do nothing but get/set the values. For example:

                  struct Person {
                      std::string name;
                      DateObject dob;
                      (...)
                  };
                  

                  如果你想象那里有 20 个以上的变量,把它写成一个包含私有成员和 40 多个访问器的类是很难管理的,对我来说似乎很浪费.

                  If you imagine 20 more variables there, writing this as a class with private members and 40-something accessors is a pain to manage and seems wasteful to me.

                  有时,我可能还需要向数据添加某种最小功能.在示例中,假设我有时也需要年龄,基于 dob:

                  Sometimes though, I might need to also add some sort of minimal functionality to the data. In the example, say I also sometimes need the age, based on dob:

                  struct Person {
                      std::string name;
                      DateObject dob;
                      (...)
                      int age() {return calculated age from dob;}
                  }
                  

                  当然,对于任何复杂的功能,我都会创建一个类,但是对于像这样的简单功能,这是糟糕的设计"吗?如果我确实使用了一个类,将数据变量保留为公共类成员是不是很糟糕,还是我只需要接受它并使用一堆访问器方法创建类?我了解类和结构之间的区别,我只是询问最佳实践.

                  Of course for any complex functionality I would make a class, but for just a simple functionality like this, is this "bad design"? If I do use a class, is it bad form to keep the data variables as public class members, or do I just need to accept it and make classes with a bunch of accessor methods? I understand the differences between classes and structs, I'm just asking about best practices.

                  推荐答案

                  我认为这里有两个重要的设计原则需要考虑:

                  I think there are two important design principles to consider here:

                  1. 如果该类有一些不变性,则通过接口隐藏该类的表示.

                  当该类存在无效状态时,该类具有不变量.类应始终保持不变.

                  A class has an invariant when there is such thing as an invalid state for that class. The class should maintain its invariant at all times.

                  考虑表示二维几何点的 Point 类型.这应该只是一个带有公共 xy 数据成员的 struct.没有无效点这样的东西.xy 值的每个组合都非常好.

                  Consider a Point type that represents a 2D geometric point. This should just be a struct with public x and y data members. There is no such thing as an invalid point. Every combination of x and y values is perfectly fine.

                  对于Person,它是否有不变量完全取决于手头的问题.您认为空名称之类的东西是有效名称吗?Person 可以有任何出生日期吗?对于你的情况,我认为答案是肯定的,你的班级应该让成员公开.

                  In the case of a Person, whether it has invariants depends entirely on the problem at hand. Do you consider such things as an empty name as a valid name? Can the Person have any date of birth? For your case, I think the answer is yes and your class should keep the members public.

                  参见:类应该强制执行不变量

                  非友元非成员函数改进封装.

                  没有理由将您的 age 函数实现为成员函数.age的结果可以通过Person的公共接口计算出来,所以没有理由成为成员函数.将它放在与 Person 相同的命名空间中,以便通过依赖于参数的查找找到它.ADL 找到的函数是该类接口的一部分;他们只是无权访问私人数据.

                  There's no reason your age function should be implemented as a member function. The result of age can be calculated using the public interface of Person, so it has no reason to be a member function. Place it in the same namespace as Person so that it is found by argument-dependent lookup. Functions found by ADL are part of the interface of that class; they just don't have access to private data.

                  如果您确实将其设为成员函数,并且有一天向Person 引入了一些私有状态,那么您将拥有不必要的依赖项.突然 age 获得了比它需要的更多的数据访问权限.

                  If you did make it a member function and one day introduced some private state to Person, you would have an unnecessary dependency. Suddenly age has more access to data than it needs.

                  参见:非成员函数如何改进封装p>

                  所以这是我将如何实现它:

                  So here's how I would implement it:

                  struct Person {
                    std::string name;
                    DateObject dob;
                  };
                  
                  int age(const Person& person) {
                    return calculated age from person.dob;
                  }
                  

                  这篇关于具有成员函数的 C++ 结构与具有公共变量的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)

                  <legend id='Ylh80'><style id='Ylh80'><dir id='Ylh80'><q id='Ylh80'></q></dir></style></legend>
                  • <small id='Ylh80'></small><noframes id='Ylh80'>

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

                      <tbody id='Ylh80'></tbody>
                      <bdo id='Ylh80'></bdo><ul id='Ylh80'></ul>

                            <tfoot id='Ylh80'></tfoot>