类和成员变量

发布于 2024-10-10 12:55:35 字数 522 浏览 3 评论 0原文

假设我有一个名为 myList 的容器类。这个容器类有一个名为capacity的私有成员变量,它保存实例中值的数量。

对于该类的用户来说,访问容量可能是有益的,例如,在循环遍历每个值时知道他们何时到达末尾。因此,容量应该是公开的。

然而,这也将允许该类的用户修改容量,这显然会把事情搞砸。

myList myInstance;
myInstance.capacity = 123;

如果公共成员函数只返回capacity的值(这是一个私有变量),这会被认为是不好的做法吗?例如:

unsigned int getCapacity()
{
    return capacity;
}

每当 capacity 的值发生变化时,“克隆”变量就会更新为 capacity 的值,这又如何呢?该类的用户将访问公共“克隆”而不是私有变量本身。

Let's say I have a container class called myList. This container class has a private member variable called capacity that holds the number of values in the instance.

It might be beneficial for users of the class to have access to capacity for, say, knowing when they've reached the end while going through each value in a loop. Therefore, capacity should be public.

However, that would also allow users of the class to modify capacity, which would obviously screw things up.

myList myInstance;
myInstance.capacity = 123;

Would it be considered bad practice to have a public member function that just returns the value of capacity, which would be a private variable? For example:

unsigned int getCapacity()
{
    return capacity;
}

What about a "clone" variable that's updated to the value of capacity whenever the value of capacity changes? Users of the class would access the public "clone" rather than the private variable itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

等风来 2024-10-17 12:55:35

有一个容量吸气剂。
但将其标记为 const 成员:

unsigned int getCapacity()  const
{                        //^^^^^^^ 
    return capacity;
}

第二种解决方案不起作用。
因为它不可靠(一个用户可能会更新它,然后下一个用户在读取它时会得到无效值)。

不过您应该考虑您班级的用户是否确实需要此信息。
他们能用它做什么?

如果容量不够,你想预先分配内存吗?

MyList   x;
// I am going to add 10 items make sure there is enough room.
if (x.size() + 10 < x.capacity())
{    x.resize(x.size() + 10);
}

在这种情况下,只需重新调整大小即可。然后,如果容器已经有空间,则不执行任何操作。

x.resize(x.size() + 10);  // If the container has enough space do nothing. 
                          // Otherwise make sure there is enough room for 10 more items.

通常,对象最好管理它自己,而不是提供对其内部状态的访问以允许其他对象间接管理它。即使您将实现与实际变量解耦,您仍然将自己耦合到具有容量的接口,并且它并没有真正为您提供有意义的好处。

因此,方法(通常)应该是对对象执行操作的操作(动词)。这些动作操纵对象的状态。让操作成为对象的接口。请注意,一旦定义了对象的接口,更改该接口(删除功能)就非常困难。

Have a capacity getter.
But mark it as a const member:

unsigned int getCapacity()  const
{                        //^^^^^^^ 
    return capacity;
}

The second solution does not work.
As it is not reliable (one user may update it then the next user would get an invalid value when they read it).

Though you should consider if the user of your class really neads this information.
What can they do with it?

So you want to pre-allocate memeory if the capacity is not enough?

MyList   x;
// I am going to add 10 items make sure there is enough room.
if (x.size() + 10 < x.capacity())
{    x.resize(x.size() + 10);
}

In this situation just always re-size. Then make your container do nothing if it aleady has the space.

x.resize(x.size() + 10);  // If the container has enough space do nothing. 
                          // Otherwise make sure there is enough room for 10 more items.

It is usually better for an object to manage it's self, rather than provide access to its internal state to allow other objects to manage it indirectly. Even though you are de-coupling the implementation from the actual variable you are still coupling yourself to the interface of having a capacity and it does not really provide you with a meaningful benefit.

So methods should (generally) be actions (verbs) that perform actions on the object. These actions manipulate the state of the object. Let the actions be the interface to your object. Note that once you define the interface to an object changing that interface (removing functionality) is very hard.

月下凄凉 2024-10-17 12:55:35

访问器是一个很好的解决方案。这是STL 选择的一个。请参见 std::vector::capacity()、std::vector::size()、...

The accessor is a good solution. This is the one choosen by the STL. See std::vector::capacity(), std::vector::size(), ...

无戏配角 2024-10-17 12:55:35

您描述的第一个解决方案称为吸气剂,拥有这些被认为是良好的做法。

第二个解决方案实际上并不是一个解决方案,它只是添加了第二个变量并引入了更新它的必要性。

The first solution you describe is known as a getter, and it is considered good practice to have these.

The second solution is not actually a solution, it merely adds a second variable and introduces the necessity to update it.

你又不是我 2024-10-17 12:55:35

不,这不是一个坏习惯。事实上,使用 getter 和 setter 被认为是最佳实践。

No that would not be a bad practice. In fact using getters and setters is considered a best practice.

夢归不見 2024-10-17 12:55:35

这根本不是一个坏习惯,这就是你实际上需要这样做的方式。

not a bad practice at all, thats how you need to do this actually.

余厌 2024-10-17 12:55:35

如果您有公共 getter,则只需返回容量的副本或 const 引用即可。

这将允许客户端查看但不能修改数据。

If you have a public getter, you can just return a copy of, or const reference to capacity.

That will allow client's to see but not modify the data.

梦中的蝴蝶 2024-10-17 12:55:35

只要您通过值或 const 指针返回 private 成员,而不是通过非常量指针返回其地址,就可以了。

As long as you return the private member by value or a const pointer instead of its address through non-const pointer you will be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文