如何传递const引用? [英] how to pass by const reference?

查看:105
本文介绍了如何传递const引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么VB.NET V2强迫我为我的set函数传递值?当我尝试将
更改为const byref时,它会给我一个语法错误。当对一个

常量字符串对象的引用会很好的时候,通过值传递字符串似乎非常有效(我们要做的就是复制它)。在

我的情况下,set函数的byval会导致多余的副本吗?

谢谢,

齐格弗里德

班级人员

dim m_name as string

public sub new(n as string)

m_name = n

end sub

公共财产名称()为字符串

获取

返回m_name

结束获取

set(const byref value as string)

m_name = value

end set

end property

公共重载覆盖函数ToString()as string

return m_name

end function

protected overloads覆盖sub Finalize()

outp.WriteLine("〜"& name)

MyBase.Finalize()

end sub

结束类

Why does VB.NET V2 force me to pass by value for my set function? When I try
to change it to const byref it gives me a syntax error. It seems very
inefficient to be passing strings around by value when a reference to a
constant string object will do fine (all we are going to do is copy it). In
my case, would the byval for the set function cause a superfluous copy?
Thanks,
Siegfried

class person
dim m_name as string
public sub new (n as string)
m_name = n
end sub
public property name () as string
Get
return m_name
end get
set ( const byref value as string)
m_name = value
end set
end Property
public overloads overrides function ToString() as string
return m_name
end function
protected overloads overrides sub Finalize()
outp.WriteLine("~" & name )
MyBase.Finalize()
end sub
End Class

推荐答案

" Siegfried Heintze" < si ******* @ heintze.com写信息

新闻:OH **************** @ TK2MSFTNGP06.phx.gbl。 ..
"Siegfried Heintze" <si*******@heintze.comwrote in message
news:OH****************@TK2MSFTNGP06.phx.gbl...

为什么VB.NET V2强迫我为我的set函数传递值?当我将
尝试将其更改为const byref时,它会给我一个语法错误。当对一个

常量字符串对象的引用会很好的时候,通过值传递字符串似乎非常有效(我们要做的就是复制它)。

在我的情况下,set函数的byval是否会导致多余的副本?
Why does VB.NET V2 force me to pass by value for my set function? When I
try to change it to const byref it gives me a syntax error. It seems very
inefficient to be passing strings around by value when a reference to a
constant string object will do fine (all we are going to do is copy it).
In my case, would the byval for the set function cause a superfluous copy?



它不会创建字符串的第二个副本,只是指向它的指针。所以

你有2个指针指向1个字符串。如果你从

1指针更改字符串,它会创建一个副本然后我相信。


Michael

It doesn''t create a second copy of the string, just the pointer to it. So
you have 2 pointers pointing to the 1 string. If you change the string from
1 pointer it will create a copy then I believe.

Michael


为什么VB.NET V2强制我为我的set函数传递值?
Why does VB.NET V2 force me to pass by value for my set function?

set(const byref value as string)
set ( const byref value as string)



我从未听说过这里使用的const关键字。


尝试跳过它。


-

Rory

I have never heard of the const keyword being used here.

Try skipping it.

--
Rory




" Michael C" < mi ** @ nospam.comwrote in message

news:u2 ************* @ TK2MSFTNGP04.phx.gbl ...

"Michael C" <mi**@nospam.comwrote in message
news:u2*************@TK2MSFTNGP04.phx.gbl...

" Siegfried Heintze" < si ******* @ heintze.com写信息

新闻:OH **************** @ TK2MSFTNGP06.phx.gbl。 ..
"Siegfried Heintze" <si*******@heintze.comwrote in message
news:OH****************@TK2MSFTNGP06.phx.gbl...

>为什么VB.NET V2强迫我为我的set函数传递值?当我尝试将其更改为const byref时,它会给我一个语法错误。当对
常量字符串对象的引用会很好的时候,通过值传递字符串似乎非常低效(我们要做的就是复制它)。
就我而言, set函数的byval会导致多余的复制吗?
>Why does VB.NET V2 force me to pass by value for my set function? When I
try to change it to const byref it gives me a syntax error. It seems very
inefficient to be passing strings around by value when a reference to a
constant string object will do fine (all we are going to do is copy it).
In my case, would the byval for the set function cause a superfluous
copy?



它不会创建字符串的第二个副本,只是指向它的指针。所以

你有2个指针指向1个字符串。如果你从1指针改变字符串

它会创建一个副本然后我相信。


It doesn''t create a second copy of the string, just the pointer to it. So
you have 2 pointers pointing to the 1 string. If you change the string
from 1 pointer it will create a copy then I believe.



这是正确的。默认情况下,VB .NET中的所有参数都传递ByVal和

传递参数ByVal意味着你得到一个副本 - 但不一定是数据的b / b
副本。如果你传递一个引用类型(字符串是)ByVal,

你得到一个指向引用类型的指针的副本,因此有两种方法可以获得
访问一个内存地址。如果你传递一个值类型(比如一个整数)

ByValue,那么你得到一个存储在堆栈上的实际数据的副本。


如果你是传递引用类型(String)ByRef,然后你会得到一个指向原始指针的

指针,它指向内存。指向

指针的指针不如第一个指针的副本效率高。

That is correct. By default all arguments in VB .NET are passes ByVal and
passing an argument ByVal means you get a copy - - but not necessarially a
copy of the data. If you pass a reference type (which a String is) ByVal,
you get a copy of the pointer to the reference type, thus having two ways to
access the one memory address. If you pass a value type (like an Integer)
ByValue, then you get a copy of the actual data stored on the stack.

If you were to pass a reference type (String) ByRef, then you''d get a
pointer to the original pointer, which points to the memory. A pointer to a
pointer is not as effiicient as a copy of the first pointer.


>

Michael
>
Michael



这篇关于如何传递const引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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