为什么const不能用于尖头物体? [英] why does const not work on pointed objects?

查看:60
本文介绍了为什么const不能用于尖头物体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


有一个很好的理由可以让const对象调用对象的非const

函数。成员指针指向。

我只是没有看到它。对我来说,这使得const关键字减少了很多

可用。

有谁可以告诉我这是什么原因?


TIA,

Corno

解决方案



" Corno" < corno@%spam%.dds.nl>在留言中写道

news:c0 ********** @ reader08.wxs.nl ...

大家好,


对象无法调用任何东西。

成员函数内的代码确实可以打电话给其他人(会员 -

同一个班级或其他班级或非会员)的职能,但是b $ b。其成员

函数的不同类型对象的常量使得调用完全无关紧要。那可能是

难以理解,我知道...... :-)

非const
它的成员指针指向的对象的函数。



只能为非对象对象调用nonconst成员函数对象。

这样可以保护以防止无意中修改const对象。


可以为const或nonconst对象调用const成员函数。

我只是没有看到它。对我来说,这使得const关键字可以使用得更少。


我发现它非常实用。

有谁可以告诉我这是什么原因?




我不能说出一些不真实的理由。 :-)


也许你可以用一些代码更清楚地证明你的问题?

(附带适当的评论)。


-Mike


Corno写道:

...
有一个很好的理由为什么一个const object可以调用其成员指针所指向的对象的非const
函数。
我只是没有看到它。对我来说,这使得const关键字的使用更少了。
任何人都能告诉我这是个好理由吗?
...



因为您正在处理两个完全不相关的对象。请你澄清一下,为什么你认为一个对象的常量(或一个对象的

访问路径)应该以某种方式暗示一个
的常量
完全不相关的其他对象(或其他对象的访问路径)?


-

祝你好运,

Andrey Tarasevich


" Corno" < corno@%spam%.dds.nl>在消息中写道

news:c0 ********** @ reader08.wxs.nl

大家好,

那里这可能是一个很好的理由,为什么const对象可以调用它的成员指针指向的对象的非const
函数。
我只是没有看到它。对我来说,这使得const关键字变得不那么有用了。
任何人都可以告诉我这是个好理由吗?

TIA,

Corno




我怀疑你混淆了两件事:


1.指针是否被指定为指向const对象。

2.指针是否为const本身。


在情况1中,你不能使用指针修改指向的东西(即

您无法更改

指针中包含的内存地址对象)。在第2种情况下,你不能修改指针本身(即,你不能

改变它,所以它指向不同的内存地址)。


有四个指针常量的可能组合,

的例子如下:


//根本不是const

A .TestClass * ptr;


//指向const对象的非const指针

B. const TestClass * ptr_to_const;


//指向非const对象的const指针

C. TestClass * const const_ptr;


//指向const对象的const指针

D. const TestClass * const const_ptr_to_const;


指针所属的类别取决于const与星号的关系

与星号的关系。如果const位于星号的左侧,那么

使它成为一个指向const对象的指针(上面的情况1)。如果指针是星号右边的
,那就使它成为一个const指针(上面的情况2)。


关于这些各种指针类型的规则如下:


非const对象的指针----上面的A和C ---可以用来调用

非const成员功能。但是,如果一个对象被声明为const

类型,那么指向非const对象的指针不能存储该对象的地址,

即尝试将const对象的地址分配给指向

的非const对象将无法编译。因此,不可能使用

指向非const对象的指针来调用const

对象上的非const成员函数。


指向const对象的指针---上面的B和D ---不能用来调用

非const成员函数但可以存储const和
$ b的地址$ b非const对象。


将这两个规则放在一起,我们看到没有指针

允许你调用非const成员const对象上的函数。这将是

似乎是尊重const资格赛的重要事项



-

John Carson

1.要回复电子邮件地址,请删除donald

2.不要回复电子邮件地址(在此处发帖)


Hi all,

There''s probably a good reason why a const object can call non const
functions of the objects where it''s member pointers point to.
I just don''t see it. For me, that makes the the const keyword a lot less
usable.
Can anybody tell me what that good reason is?

TIA,

Corno

解决方案


"Corno" <corno@%spam%.dds.nl> wrote in message
news:c0**********@reader08.wxs.nl...

Hi all,

There''s probably a good reason why a const object can call
Objects cannot ''call'' anything.
Code inside a member function can indeed call other (member --
either same class or other class--- or nonmember) functions,
though. The constness of a different type object whose member
function makes the call doesn''t matter at all. That''s probably
incomprehensible, I know... :-)
non const
functions of the objects where it''s member pointers point to.

A nonconst member function object can be called only for nonconst objects.
This ''protects'' against inadvertent modification of a const object.

A const member function can be called for either const or nonconst objects.
I just don''t see it. For me, that makes the the const keyword a lot less
usable.
I find it quite usable.
Can anybody tell me what that good reason is?



I cannot give a reason for something which isn''t true. :-)

Perhaps you can demonstrate your questions more clear with some code?
(with appropriate accompanying comments).

-Mike


Corno wrote:

...
There''s probably a good reason why a const object can call non const
functions of the objects where it''s member pointers point to.
I just don''t see it. For me, that makes the the const keyword a lot less
usable.
Can anybody tell me what that good reason is?
...



Because you are dealing with two completely unrelated objects. Would you
please clarify, why do you think that constness of one object (or of an
access path to one object) should somehow implicate constness of a
completely unrelated other object (or of an access path to other object)?

--
Best regards,
Andrey Tarasevich


"Corno" <corno@%spam%.dds.nl> wrote in message
news:c0**********@reader08.wxs.nl

Hi all,

There''s probably a good reason why a const object can call non const
functions of the objects where it''s member pointers point to.
I just don''t see it. For me, that makes the the const keyword a lot
less usable.
Can anybody tell me what that good reason is?

TIA,

Corno



I suspect that you are confusing two things:

1. Whether a pointer is typed as pointing to a const object.
2. Whether a pointer is itself const.

In case 1, you cannot use the pointer to modify what is pointed to (i.e.,
you cannot change the object at the memory address contained in the
pointer). In case 2, you cannot modify the pointer itself (i.e., you cannot
change it so it points to a different memory address).

There are four possible combinations of pointer const-ness, examples of
which are given below:

// not const at all
A. TestClass * ptr;

// non-const pointer to a const object
B. const TestClass * ptr_to_const;

// const pointer to non-const object
C. TestClass * const const_ptr;

// const pointer to const object
D. const TestClass * const const_ptr_to_const;

Which category a pointer falls into depends on where the const is in
relation to the asterisk. If the const is to the left of the asterisk, that
makes it a pointer to a const object (case 1 above). If the pointer is to
the right of the asterisk, that makes it a const pointer (case 2 above).

The rules regarding these various pointer types are as follows:

Pointers to non-const objects ---- A and C above --- can be used to call
non-const member functions. However, if a object is declared to be of const
type, then a pointer to non-const objects cannot store the object''s address,
i.e., an attempt to assign the address of a const object to a pointer to
non-const objects will fail to compile. Thus it is not possible to use
pointers to non-const objects to call non-const member functions on a const
object.

Pointers to const objects --- B and D above --- cannot be used to call
non-const member functions but can store the address of both const and
non-const objects.

Putting these two rules together, we see that there is no pointer that
allows you to call a non-const member function on a const object. This would
seem to be the thing that matters as far as respecting const qualifiers
goes.
--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)


这篇关于为什么const不能用于尖头物体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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