Const继承 [英] Const inheriting

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

问题描述

大家好,


每个人对const继承的看法是什么?

指针指向的对象是否应该继承指针的常量?比如

,如果一个类有一个指针然后解除引用那个

指针。由于指针与类对象具有相同的常量,解除引用的指针是否具有相同的

指针的常量?


是的,我知道C ++不这样做。我只想知道

每个人对此主题的研究意见。


感谢您的参与,

Adrian

-

__________________________________________________ ___________________

\ / Adrian_Hawryluk BSc。 - 专长:UML,OOPD,实时系统\ /

\我的新闻组作品根据知识共享许可/

\署名 - 非商业性使用 - 相同方式3.0许可证/

\ _____ [ http://creativecommons.org/licenses/...sa/3.0/] _____ /

\ / ______ [blog:__http:// adrians-musings。 blogspot.com/]______ \ /

Hi all,

What is everyone''s opinion of const inheriting? Should the object that
a pointer is pointing at inherit the constness of the pointer? Such as
in the case of a class having a pointer and then dereferencing that
pointer. Should the dereferenced pointer have the same constness of the
pointer as the pointer has the same constness as the class object?

Yes, I am aware that C++ does not do this. I just want to know
everyones opinion about this subject for research.

Thanks for your participation,
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/

推荐答案

Adrian Hawryluk写道:
Adrian Hawryluk wrote:

每个人对const继承的看法是什么?指针指向的对象

是否应该继承指针的常量?这样的

就像一个有一个指针的类然后

解除引用那个指针一样。如果指针具有相同的常量

作为类对象,则解除引用的指针是否具有指针的相同常量?

是的,我我知道C ++不会这样做。我只想知道每个人对此主题的研究意见。

What is everyone''s opinion of const inheriting? Should the object
that a pointer is pointing at inherit the constness of the pointer? Such
as in the case of a class having a pointer and then
dereferencing that pointer. Should the dereferenced pointer have the
same constness of the pointer as the pointer has the same constness
as the class object?
Yes, I am aware that C++ does not do this. I just want to know
everyones opinion about this subject for research.



我不认为这是可行的限制。作为一个选项,它可能是有用的,但是你已经有了这个,如果你明确地说明了
在指针本身的声明中使对象为const:


类型const * const pointer_to_object;


(而不是


object * const pointer_to_object;)


应该给你一个const对象


decltype(* pointer_to_object)& ref_to_object = * pointer_to_object;


(decltype会给''type const'',看看关于''decltype''的提议)。


如果我误解了,我很抱歉。也许你可以提供一个例子

你想要完成的建议。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

I do not think this is viable as a restriction. As an option it would
likely be useful, but then you have this already, if you explicitly
make the object const in the declaration of the pointer itself:

type const * const pointer_to_object;

(instead of

object * const pointer_to_object; )

which should give you a const object

decltype(*pointer_to_object) & ref_to_object = *pointer_to_object;

(decltype would give ''type const'', see a proposal on ''decltype'').

If I misunderstood, I am sorry. Perhaps you could supply an example
of what you''d like your proposal to accomplish.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Adrian Hawryluk写道:
Adrian Hawryluk wrote:

大家好,


什么是每个人'对const继承的看法?

指针指向的对象是否应该继承指针的常量?比如

,如果一个类有一个指针然后解除引用那个

指针。由于指针与类对象具有相同的常量,因此解除引用的指针是否具有相同的

指针的常量?
Hi all,

What is everyone''s opinion of const inheriting? Should the object that
a pointer is pointing at inherit the constness of the pointer? Such as
in the case of a class having a pointer and then dereferencing that
pointer. Should the dereferenced pointer have the same constness of the
pointer as the pointer has the same constness as the class object?



No.

No.


是的,我知道C ++不会这样做。我只想知道每个人对此主题的研究意见。

Yes, I am aware that C++ does not do this. I just want to know
everyones opinion about this subject for research.



什么样的研究?

Best


Kai-Uwe Bux

What kind of research?
Best

Kai-Uwe Bux


Victor Bazarov写道:
Victor Bazarov wrote:

Adrian Hawryluk写道:
Adrian Hawryluk wrote:

>每个人对const继承的看法是什么?指针指向的对象是否应该继承指针的常量?这样的
就像一个类有一个指针,然后取消引用该指针。取消引用的指针是否具有指针的相同常量,因为指针与类对象具有相同的常量?
是的,我知道C ++不会这样做。我只是想知道每个人对此主题的研究意见。
>What is everyone''s opinion of const inheriting? Should the object
that a pointer is pointing at inherit the constness of the pointer? Such
as in the case of a class having a pointer and then
dereferencing that pointer. Should the dereferenced pointer have the
same constness of the pointer as the pointer has the same constness
as the class object?
Yes, I am aware that C++ does not do this. I just want to know
everyones opinion about this subject for research.



我不认为这是一个可行的限制。作为一个选项,它可能是有用的,但是你已经有了这个,如果你明确地说明了
在指针本身的声明中使对象为const:


类型const * const pointer_to_object;


(而不是


object * const pointer_to_object;)


应该给你一个const对象


decltype(* pointer_to_object)& ref_to_object = * pointer_to_object;


(decltype会给''type const'',看看关于''decltype''的提议)。


如果我误解了,我很抱歉。也许你可以提供一个你想要完成的建议的例子。


I do not think this is viable as a restriction. As an option it would
likely be useful, but then you have this already, if you explicitly
make the object const in the declaration of the pointer itself:

type const * const pointer_to_object;

(instead of

object * const pointer_to_object; )

which should give you a const object

decltype(*pointer_to_object) & ref_to_object = *pointer_to_object;

(decltype would give ''type const'', see a proposal on ''decltype'').

If I misunderstood, I am sorry. Perhaps you could supply an example
of what you''d like your proposal to accomplish.



好​​吧,堆栈上的指针确实有一些原因不能继续(?)/提升(?)constness到它的类型吗?你指的是你可能希望编译器知道(并且稍后不允许

程序员)修改指针的原因,同时仍允许

指向对象的修改。我同意这一点。


我建议的是当他们在class / struts中使用时。这是

的一个例子:


class class

{

int * pNumber;

public:

Class():pNumber(new int(5)){}


//假定正确的复制构造函数或难以接近的一个


T * number()const {return pNumber; } //没有被标记为错误

};


是的,这是一个人为的简单例子并且很容易被修复,但它是

是显示一个点。程序员/可能会无意中/通过

错误执行此操作。然后其他一些程序员通过将pNumber指向的

值改为6来利用它。


编译器是否允许这样做?我听说有人说它不会因为它打破了const的正确性,允许那些对某个对象有b $ ba处理的人能够间接改变对象的状态

,这样如果从同一个const句柄多次访问,调用const成员可能会导致

不相同。注意

我忽略了const句柄的发起者可能

有一个非常量句柄的事实,并且可能会在任何
时间。这可能是M

Ok, pointers on the stack do have some reasons for not
inheriting(?)/promoting(?) constness to the type it is pointing at, for
the reason that you may want the compiler to know (and disallow the
programmer later on) from modifying the pointer, while still allowing
modifications of the object pointed at. I agree with that.

What I am suggesting is when they are used in classes/struts. Here is
an example:

class Class
{
int * pNumber;
public:
Class() : pNumber(new int(5)) {}

// assume either proper copy constructor or inaccessible one

T* number() const { return pNumber; } // not flagged as error
};

Yes, this is a contrived trivial example and is easily ''fixed'', but it
is to show a point. A programmer /may inadvertently/ do this by
mistake. And then some other programmer exploits it, by changing the
value pointed at by pNumber to, say 6.

Should this be allowed by the compiler? I''ve heard that some say it
shouldn''t because it breaks const correctness, allowing someone who has
a const ''handle'' to an object to be able to indirectly change the state
of the object, such that calling a const member may result in not the
same value if accessed more than once from the same const handle. Note
that I am ignoring the fact that the originator of the const handle may
have a non-const handle and may change that object''s internals at any
time. This is probably why M


这篇关于Const继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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