封装和运算符[] [英] Encapsulation and Operator[]

查看:89
本文介绍了封装和运算符[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到operator []实现了这样的事情:


class Foo {...};


class FooList

{

public:

const Foo& operator [](无符号索引)const {return

array [index];};

Foo& operator [](unsigned index){return

array [index];};

private:

Foo array [num];

};


这看起来自然而直观(至少对我而言)。但似乎

破坏封装。是否有一些标准方法可以避免这种违规行为,并且仍然为客户提供一个自然且易于使用的界面?b $ b b b b b b b b b b b b b b b或者你只是咬紧牙关接受它?

请向那些知识水平高的人做出回应

一年的C ++经验。


谢谢,


罗杰


I often see operator[] implemented something like this:

class Foo { ... };

class FooList
{
public:
const Foo& operator[] (unsigned index) const {return
array[index];};
Foo& operator[] (unsigned index) {return
array[index];};
private:
Foo array[num];
};

And this seems natural and intuitive (at least to me). But it seems to
wreck encapsulation. Is there some standard way to avoid this
transgression and still provide the client with an interface that is
natural and easy to use? Or do you just bite the bullet and accept it?
Please pitch responses to someone whose level of knowledge is about
one year of C++ experience.

Thank you,

Roger



推荐答案

On Fri,2006年3月17日21:57:20 -0800,Roger Lakner

< rl ***** @ adelphia.net>写道:
On Fri, 17 Mar 2006 21:57:20 -0800, "Roger Lakner"
<rl*****@adelphia.net> wrote:
我经常看到operator []实现了这样的东西:

类Foo {...};

类FooList
{
公开:
const Foo& operator [](无符号索引)const {return
array [index];};
Foo& operator [](unsigned index){return
array [index];};
private:
Foo array [num];
};

这看起来自然而直观(至少对我而言)。但它似乎破坏了封装。是否有一些标准方法可以避免这种违规行为并仍然为客户提供一个自然且易于使用的界面?或者你只是咬紧牙关接受它?
请向那些知识水平相当于一年C ++经验的人做出回应。
I often see operator[] implemented something like this:

class Foo { ... };

class FooList
{
public:
const Foo& operator[] (unsigned index) const {return
array[index];};
Foo& operator[] (unsigned index) {return
array[index];};
private:
Foo array[num];
};

And this seems natural and intuitive (at least to me). But it seems to
wreck encapsulation. Is there some standard way to avoid this
transgression and still provide the client with an interface that is
natural and easy to use? Or do you just bite the bullet and accept it?
Please pitch responses to someone whose level of knowledge is about
one year of C++ experience.




为什么你说它破坏了封装?不可否认,上面显示的

实现并没有给你带来任何超过直接公共访问数组成员变量的东西,但这并不意味着它是

不能以不同的方式完成。


例如,如果index超出范围,则可以抛出异常。

实现也可能非常复杂。考虑到即使是成员数组也可能没有

,但是operator []执行数据库查找

(不知何故)。或者真正的数组保存在另一个类中,
和FooList保存一个指向该类的指针或引用,它将转发该调用。这里有很多种可能性。


-

Bob Hairgrove
没有********** @ Home.com




Roger Lakner < RL ***** @ adelphia.net> skrev i meddelandet

新闻:Tq ******************** @ adelphia.com ...

"Roger Lakner" <rl*****@adelphia.net> skrev i meddelandet
news:Tq********************@adelphia.com...
我经常看到operator []实现了这样的东西:

类Foo {...};

类FooList
{
公开: const Foo& operator [](无符号索引)const {return
array [index];};
Foo& operator [](unsigned index){return
array [index];};
private:
Foo array [num];
};

这看起来自然而直观(至少对我而言)。但似乎
破坏了封装。是否有一些标准方法可以避免这种违规行为并仍然为客户提供一个自然且易于使用的界面?


这是界面的一个重要特征!

或者你只是咬紧牙关接受它?


这不是关于接受与否,而是关于你想要做什么来对付一个对象。在不知道Foo是什么的情况下,很难判断是否
FooList f;


f [5] = someFoo;


是自然的或不。这取决于!


考虑到C ++使用这种

接口提供了std :: vector,它在某些时候必须是正确的。 :-)


请对那些知识水平相当于一年C ++经验的人做出回应。
I often see operator[] implemented something like this:

class Foo { ... };

class FooList
{
public:
const Foo& operator[] (unsigned index) const {return
array[index];};
Foo& operator[] (unsigned index) {return
array[index];};
private:
Foo array[num];
};

And this seems natural and intuitive (at least to me). But it seems
to
wreck encapsulation. Is there some standard way to avoid this
transgression and still provide the client with an interface that is
natural and easy to use?
That is an important feature for an interface!
Or do you just bite the bullet and accept it?
It is not about accepting or not, it is about what you want to do to
an object. Without knowing what a Foo is, it is hard to tell if

FooList f;

f[5] = someFoo;

is "natural" or not. It depends!

Considering that C++ provides a std::vector with this kind of
interface, it must be correct some of the time. :-)

Please pitch responses to someone whose level of knowledge is about
one year of C++ experience.




对于所有情况,并不总是普遍的规则。你需要

单独考虑每一个。

Bo Persson



There aren''t always universal rules for all situations. You have to
consider each one individually.
Bo Persson




Roger Lakner写道:

Roger Lakner wrote:
我经常看到operator []实现了这样的事情:

类Foo {...};

类FooList
{
公众:
const Foo& operator [](无符号索引)const {return
array [index];};
Foo& operator [](unsigned index){return
array [index];};
private:
Foo array [num];
};

这看起来自然而直观(至少对我而言)。但它似乎破坏了封装。是否有一些标准方法可以避免这种违规行为并仍然为客户提供一个自然且易于使用的界面?或者你只是咬紧牙关并接受它?
I often see operator[] implemented something like this:

class Foo { ... };

class FooList
{
public:
const Foo& operator[] (unsigned index) const {return
array[index];};
Foo& operator[] (unsigned index) {return
array[index];};
private:
Foo array[num];
};

And this seems natural and intuitive (at least to me). But it seems to
wreck encapsulation. Is there some standard way to avoid this
transgression and still provide the client with an interface that is
natural and easy to use? Or do you just bite the bullet and accept it?




Wrecks encapsulation?相反,FooList完全展示了一个类应该如何封装其数据 - 私有数据成员和

公共接口。请注意,客户端无法直接访问FooList的数据

成员,而是必须调用FooList的public

接口中的方法来访问FooList的数据。换句话说,FooList通过调解对它的所有访问来封装其数据。


封装使FooList可以改变它的基础

存储模型不会影响其客户端 - 而且质量是封装的主要好处。例如,我们可以设想一个

实现,其中FooList访问网络服务器,或者

数据库,或者其他类型的商店来检索Foo对象
运营商[]返回
。对客户端来说,这个对FooList的更改将会被检测到 - > b $ b未被检测到 - 因为即使它的数据表示可能已经改变了b $ b - 它的客户端都使用它的公共接口 - 不会

已经改变。


Greg



"Wrecks encapsulation?" On the contrary, FooList demonstrates exactly
how a class should encapsulate its data - with private data members and
a public interface. Note that clients cannot access FooList''s data
member directly, instead they must invoke methods in FooList''s public
interface to access FooList''s data. In other words, FooList has
encapsulated its data by mediating all access to it.

Encapsulation makes it possible for FooList to change its underlying
storage model without affecting its clients - and that quality is the
primary benefit of encapsulation. For instance we could imagine an
implementation in which FooList accessed a network server, or a
database, or some other kind of store to retrieve the Foo object
returned by operator[]. To the client, this change to FooList would go
undetected - because even though its data representation may have
changed - its public interface, which its clients all use - would not
have changed.

Greg


这篇关于封装和运算符[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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