只读数据成员 [英] Read-only data members

查看:52
本文介绍了只读数据成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法将数据成员表示为C ++中的只读状态是什么?b $ b现在已经让我(稍微)懊恼了一段时间。使用返回const引用的访问器方法,你总是可以得到它周围的

,但是带有大量访问器列表的
类通常看起来很笨拙/丑陋。

所以我终于坐下来想到了一个我希望看作

修复的语法。我当然不希望看到它被引入,但它是有趣的想法。


语法是添加一个新的关键字,公开,只能在类声明中使用

。在暴露之后,你重新声明一个现有的类

成员,具有更高的持续性,处于不同的保护状态

级别。可能有必要在那个

成员的声明之后只公开一个成员,但是对于这个例子,我假设你可以使用它之前




类Foo {

public:

// public可以读取字符串内容

公开const char * const str;

protected:

//派生类可以修改字符串内容

公开char * const str;

私人:

//只有Foo可以改变str本身

char * str;

};


我对小组的问题是:

1)编译器在处理这种语法及其

效果时会遇到什么问题吗?它似乎相对容易处理,但

我不太了解编译器。

2)语法是否有任何缺陷或它的影响?也就是说它会导致任何模棱两可的情况或产生不良副作用?

3)你认为,如果这种语法被神奇地添加到C ++中,那么它是

会是一件好事/有用事还是一件坏事/无用事?


-Dave

解决方案



" David Gausebeck" < GA ****** @ paypal.com>在消息中写道

news:3f **************** @ news.cis.dfn.de ...

无法将数据成员表示为C ++中的只读状态,这让我(稍微)一段时间感到恼火。




我无法忍受从你的帖子中了解为什么const没有提供只读

访问权限。


好点。我破解了代码


< David Gausebeck>

class Foo {

public:

// public可以读取字符串内容

公开const char * const str;

protected:

//派生类可以修改字符串内容

公开char * const str;

private:

//只有Foo才能改变str本身

char * str;

};

< / David Gausebeck>


公开只适用于受保护的,不是吗?

在公共区域,数据已经暴露了。

在私人部分,没有什么可以曝光。

所以它装扮受到保护。更合乎逻辑地介绍

处理受保护数据成员的第4个访问说明符。

它必须以p开头(我猜)。也许

特权?

-X


>>无法在C ++中将数据成员表示为只读是

,这让我(稍微)惹恼了一段时间。



我无法从你的帖子中理解为什么const没有提供只读的访问权限。




确实如此,但不是我在谈论的方式。我意识到这一行

还不清楚,但我想不出一个更好,更简洁的方式来写它的b $ b。所以我继续下面详细解释。现在有了更多的想法,它会更好,因为:


无法将数据成员表示为只读为公共或

受保护的访问是让我(稍微)为现在的

烦恼的。


-Dave


The inability to denote data members as read-only in C++ is something
which has annoyed me (slightly) for a while now. You can always get
around it with accessor methods that return const references, but
classes with huge lists of accessors often seem awkward/ugly.

So I finally sat down and thought of a syntax I''d like to see as a
fix. I certainly don''t expect to ever see it introduced, but it''s
interesting to think about.

The syntax is to add a new keyword, "expose", which can only be used
in class declarations. After expose, you redeclare an existing class
member with a greater degree of const-ness, at a different protection
level. It might be necessary to only expose a member after that
member''s declaration, but for this example I''ll assume you can use it
before:

class Foo {
public:
// public can read the string contents
expose const char* const str;
protected:
// derived classes can modify the string contents
expose char* const str;
private:
// only Foo can change str itself
char* str;
};

My questions for the group are:
1) Would compilers have any problem handling this syntax and its
effects? It seems like it would be relatively easy to handle, but
I don''t know all that much about compilers.
2) Are there any flaws with the syntax or its effects? i.e. would it
lead to any ambiguous situations or have undesirable side effects?
3) Do you think that, if this syntax were magically added to C++, it
would be a good/useful thing or a bad/useless thing?

-Dave

解决方案


"David Gausebeck" <ga******@paypal.com> wrote in message
news:3f****************@news.cis.dfn.de...

The inability to denote data members as read-only in C++ is something
which has annoyed me (slightly) for a while now.



I couldn''t understand from your post why "const" doesn''t provide read-only
access.


Nice point. I hacked out the code

<David Gausebeck>
class Foo {
public:
// public can read the string contents
expose const char* const str;
protected:
// derived classes can modify the string contents
expose char* const str;
private:
// only Foo can change str itself
char* str;
};
</David Gausebeck>

Expose only works for protected, doesn''t it?
In the public section, data is already ''exposed''.
In the private section, there is nothing to expose.
So it dresses up protected. More logical to introduce
a 4th access specifier to handle ''protected'' data members.
It must be something starting with a p (I guess). Perhaps
priviliged?

-X


>> The inability to denote data members as read-only in C++ is something

which has annoyed me (slightly) for a while now.



I couldn''t understand from your post why "const" doesn''t provide read-only
access.



It does, but not in the way I''m talking about. I realized that line
was unclear, but I couldn''t think of a better, concise way to write
it. So I just went on and explained in more detail below. Having
thought about it a little more now, it would be better as:

The inability to denote data members as read-only for public or
protected access is something which has annoyed me (slightly) for a
while now.

-Dave


这篇关于只读数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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