const参数 [英] const in argument
问题描述
嗨:
当我们声明如下内容:
func(const class a)
我们在func期间保护变量a不被修改...对吗?好吧
第一个问题:如果a是按值传递的,谁在乎它是否被修改了?!!现在,
,我们做什么
func(const class& a)或
func(const class * a)
那么,在任何一种情况下,都是一个开始受保护的?或指向一个指针?
谢谢!
>但是a是指针。被保护的是一个点(或指向)的对象。这是一个非常重要的考虑因素,因为如果调用func将修改一个func的调用者
肯定会感兴趣。
我的意思当然。
如果调用func将修改一个点(或引用)的对象。
john
< blockquote>Matthew Monopole <毫安******* @ hotmail.com>在消息中写道
news:zk ****************** @ newsread1.prod.itd.earth link.net ...
当我们声明类似的东西:
func(const class a)
我们保护变量a在func期间不被修改......对吗?
你写的不是合法的C ++,但我认为你的意思是这样的:
void func(const Foo a) ;
第一个问题:如果a是按值传递的,谁在乎它是否被修改了?? !!
可能没有人。制作一个参数const并没有多大意义,如果它是调用者参数的副本。
现在,我们做什么呢? /> func(const class& a)或
func(const class * a)
那么,在任何一种情况下,都是一个开始受保护的?或指向?的指针?
在第一种情况下,a是对调用者传递的对象的引用。
这里,const,意味着你不能用a来修改那个对象。
在第二种情况下,a是一个指针,它包含一个传递的地址副本
来电者。这里是const。意味着如果你通过a访问它,你就不能修改
分的对象。一个本身不是常数;如果你愿意,你可以修改它的价值
。由于a保存了调用者指针的副本,因此当您更改时,
调用者的指针不会被修改。
问候,
Russell Hanneken
rh*******@pobox.com
>但是a是指针。被保护的是一个点(或指向)的对象。这是一个非常重要的考虑因素,因为如果调用func将修改一个func的调用者
肯定会感兴趣。
我的意思当然。
如果调用func将修改一个点(或引用)的对象。
john
hi:
When we declare something like:
func(const class a)
we protect the variable a from being modified during func...right? okay
first question: if a is passed by value, who care if it''s modified??!!
now, what about we do
func(const class& a) or
func(const class* a)
then, in either case, is a begin protected? or a pointer that points to a?
thankx!
>But a is the pointer. What is being protected is the object that a points
(or refers) to. This is a very important consideration because the caller of func certainly is interested if calling func will modify a.
I mean of course.
if calling func will modify the object that a points (or refers) to.
john
"Matthew Monopole" <ma*******@hotmail.com> wrote in message
news:zk******************@newsread1.prod.itd.earth link.net...
When we declare something like:
func(const class a)
we protect the variable a from being modified during func...right?
What you wrote isn''t legal C++, but I assume you mean something like this:
void func (const Foo a);
first question: if a is passed by value, who care if it''s modified??!!
Probably no one. There isn''t much point in making a parameter const if it''s
a copy of the caller''s argument.
now, what about we do
func(const class& a) or
func(const class* a)
then, in either case, is a begin protected? or a pointer that points to a?
In the first case, a is a reference to an object passed by the caller.
Here, the "const" means that you can''t use a to modify that object.
In the second case, a is a pointer which holds a copy of an address passed
by the caller. Here "const" means that you can''t modify the object to which
a points if you access it through a. a itself is not const; you can modify
its value if you want. Since a holds a copy of the caller''s pointer, the
caller''s pointer is not modified when you change a.
Regards,
Russell Hanneken
rh*******@pobox.com
>But a is the pointer. What is being protected is the object that a points
(or refers) to. This is a very important consideration because the caller of func certainly is interested if calling func will modify a.
I mean of course.
if calling func will modify the object that a points (or refers) to.
john
这篇关于const参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!