有没有办法从函数内部更改外部对象
是否可以定义一个函数,使参数在返回后引用另一个(已经存在的)对象而不使用指针等?同样,这不能仅仅通过使用复制构造函数或赋值运算符或任何东西来更改现有对象。当函数返回时,外部对象将引用不同的内存块。
例如:
int x;
void change(int& blah) {
blah = x; // I don't want to change blah's value to x's value, I want to make blah refer to x outside the function
}
void main() {
int something = 0;
change(something);
assert(&x == &something);
}
无论使用哪种方法,都必须像
change(x);
在调用函数之前不对参数应用任何特殊运算符或函数一样来调用该函数。我不知道这是否可行,但如果可行的话,这将使非常酷的事情成为可能。如果不是,我也想知道为什么。
Is it possible to define a function which will make the argument refer to another (already existing) object after it returns without using pointers, etc? Again, this can't just change the existing object by using a copy constructor or assignment operator or anything. The outside object would refer to a different block of memory when the function returned.
For example:
int x;
void change(int& blah) {
blah = x; // I don't want to change blah's value to x's value, I want to make blah refer to x outside the function
}
void main() {
int something = 0;
change(something);
assert(&x == &something);
}
Regardless of the method used, the function must be called like
change(x);
Without applying any special operator or function to the argument before calling the function. I don't know if this is possible, but it would make very cool things possible if it is. If it's not, I would also like to know why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,因为
something
和x
是不同的对象。它们并不指代不同的对象。它们并不指向不同的对象。它们是不同的对象。要改变某个东西指向的位置,该东西需要是一个指针。如果你有指针,你可以这样做:
No, because
something
andx
are different objects. They don't refer to different objects. They don't point to different objects. They are different objects.To change where something points, that something needs to be a pointer. If you have a pointer, you can do this:
不,绝对不可能。像
int x
这样的常规变量和像int &y(...)
这样的引用变量以及数组都是不可重新安装的,即它们总是使用/指向同一块内存。要获得所需的功能,您确实需要使用指针或指针的抽象。否则是不可能的。
作为解释的尝试,这里是(不完全正确,并且非常简化,但足以理解这个想法):
当您声明像
int x
这样的变量时,您实际上是在要求编译器将x
与特定的内存区域相关联。例如,假设 x 与从 0x439FC2 开始的四字节相关联。由于编译器知道x
应始终引用0x439FC2
,因此任何x
的使用都可以通过查找这些内存单元来替换,并且将它们加载到寄存器中,将它们推入堆栈,或者其他什么。不管怎样,最终结果是变量名x
几乎被数字0x439FC2
取代。因此,您无法移动x
的原因是您无法使该内存地址引用内存中的不同位置。同样,该解释是简化的,并不完全正确,但它是推理自动分配变量的“直观”方式。
Nope definitely not possible. Both regular variables like
int x
and references variables likeint &y(...)
and arrays are not reseatable, i.e. they always will use/point to the same chunk of memory.To obtain the functionality you require, you really need to either use a pointer, or an abstraction of pointers. It's not possible otherwise.
As an attempt at an explanation, here goes (not entirely correct, and grossly simplified, but good enough to get the idea):
When you declare a variable like
int x
, you are really asking for the compiler to associatex
with a specific region of memory. So, for example, supposex
is associated the four-byte starting at0x439FC2
. Since the compiler knows thatx
should always refer to0x439FC2
, than any use ofx
can really be replaced by looking up those memory cells, and loading them into registers, pushing them onto the stack, or whatever. Anyways, the end result is that the variable namex
is pretty much replaced by the number0x439FC2
. So the reason you can't movex
around is that you can't make that memory address refer to a different location in memory.Again, that explanation is simplified and not entirely true, but it is the "intuitive" way of reasoning about automatically allocated variables.
您想使用对指针的引用:
You want to use a reference to a pointer: