带有成员函数的C ++结构与带有公共变量的类 [英] C++ Structs with Member Functions vs. Classes with Public Variables

查看:117
本文介绍了带有成员函数的C ++结构与带有公共变量的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的是一个良好的形式/最佳实践的问题。我使用C ++中的structs来形成基本上保存数据的对象,而不是使用一堆除了获取/设置值的访问器方法。例如:

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. 通过接口隐藏类的表示,如果该类有一些不变的话。 / strong>

  1. Hide a class's representation through an interface if there is some invariant on that class.

当类有一个无效状态时,类有一个不变量。

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.

考虑一个表示2D几何点的 Point 类型。这应该只是一个 struct with public x y 数据成员。没有这样的事情作为一个无效点。 x y 值的每个组合都是完美的。

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 的情况下,它是否具有不变量完全取决于当前的问题。你认为一个空名字这样的东西是一个有效的名字吗? 可以有任何出生日期吗?对于你的情况,我认为答案是肯定的,你的课程应该保持成员公开。

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.

查看: /

See: Classes Should Enforce Invariants

非朋友非成员函数改善了封装。

没有理由你的 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.

如果你确实是一个成员函数,并且有一天向 / code>,你会有一个不必要的依赖。突然, 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.

请参阅:非会员功能改善封装

下面是我如何实现它:

struct Person {
  std::string name;
  DateObject dob;
};

int age(const Person& person) {
  return calculated age from person.dob;
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆