通过引用传递常量 [英] Passing a constant by reference
问题描述
大家好,
我想调用一个带有void *参数的函数,并将它的地址传递给
一个常数。有没有办法在没有明确定义常量
然后传递常量的地址的情况下执行此操作?换句话说,我不想* b $ b想要做的是:
const int myInt = 5;
MyFunc(& myInt);
我想做更多这样的事情:
MyFunct(&(int)5 );
TIA - Bob
Hi all,
I want to call a function that takes a void* argument and pass it the address of
a constant. Is there a way to do this without explicitly defining the constant
and then passing the address of the constant? In other words, what I *don''t*
want to do is this:
const int myInt = 5;
MyFunc(&myInt);
I want to do something more like this:
MyFunct(&(int)5);
TIA - Bob
推荐答案
Bob Altman写道:
Bob Altman wrote:
我想调用一个带有void *参数的函数,并将其传递给
a常量的地址。有没有办法在没有明确定义常量
然后传递常量的地址的情况下执行此操作?换句话说,我不想* b $ b想要做的是:
const int myInt = 5;
MyFunc(& myInt);
我想做更多这样的事情:
MyFunct(&(int)5 );
I want to call a function that takes a void* argument and pass it the address of
a constant. Is there a way to do this without explicitly defining the constant
and then passing the address of the constant? In other words, what I *don''t*
want to do is this:
const int myInt = 5;
MyFunc(&myInt);
I want to do something more like this:
MyFunct(&(int)5);
常量表达式没有地址;你所要求的是没有意义的。
这就像询问数字5存储在计算机中的位置一样。它不是存储在任何地方的
,只是*是*。存储位置产生地址,而不是
常数值。你不小心把它与一个字面的
地址混淆,即
MyFunc((void *)5);
这会将整数5转换为内存位置并将其作为
指针传递。这将很可能在访问时产生访问冲突,因为5和5之后是b $ b。很少是有效的地址,但如果是void *仅仅意味着
持有一个指针大小的值而不是一个实际的指针,这可能有一些意义
。
-
J.
http://symbolsprose.blogspot。 com
Bob Altman写道:
Bob Altman wrote:
大家好,
>
我想调用一个带有void *参数的函数,并将它传递给一个常量的
地址。有没有办法在没有明确定义常量然后传递常量地址的情况下明确
?用
换句话说,我不想*做的是:
const int myInt = 5;
MyFunc(& myInt);
Hi all,
I want to call a function that takes a void* argument and pass it the
address of a constant. Is there a way to do this without explicitly
defining the constant and then passing the address of the constant? In
other words, what I *don''t* want to do is this:
const int myInt = 5;
MyFunc(&myInt);
那不行。你的函数是否实际上接受了const void *
而不是void *?
That won''t work. Does your function actually accept a "const void *"
instead of a "void *"?
>
我想做更多这样的事情:
MyFunct(&(int)5);
TIA - Bob
>
I want to do something more like this:
MyFunct(&(int)5);
TIA - Bob
>大家好,
>Hi all,
>>
我想调用一个带有void *参数的函数,并将其传递给常量的
地址。有没有办法在没有明确定义常量然后传递常量的地址的情况下执行此操作?用其他的话来说,我不想做的就是:
const int myInt = 5;
MyFunc(& myInt);
>>
I want to call a function that takes a void* argument and pass it the
address of a constant. Is there a way to do this without explicitly
defining the constant and then passing the address of the constant? In other
words, what I *don''t* want to do is this:
const int myInt = 5;
MyFunc(&myInt);
那不行。你的函数实际上是否接受const void *相反
的void *?
That won''t work. Does your function actually accept a "const void *" instead
of a "void *"?
是的,我的函数采用const void *
Yes, my function takes a "const void*"
这篇关于通过引用传递常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!