Imagine the following scenario:
想象一下以下场景:
class ABC
{
public:
int abc;
};
ABC& modifyABC(ABC& foo)
{
foo.abc+=1337;
return foo;
}
void saveABC(ABC& bar, std::vector<ABC*>& list)
{
list.push_back(&modifyABC(bar));
}
int main()
{
ABC foobar;
std::vector<ABC*> ABCList;
saveABC(foobar,ABCList);
return 0;
}
modifyABC() returns a reference to ABC(which is internally some sort of pointer too AFAIK). Does the "adress of" & operator now return a pointer to the adress of the reference or the actually object behind the reference?
modifyABC()返回对ABC的引用(内部某种指针也是AFAIK)。 “地址”运算符现在是否返回指向引用地址或引用后面的实际对象的指针?
5 个解决方案
#1
1
modifyABC()
returns a reference toABC
(which is internally some sort of pointer too AFAIK)modifyABC()返回对ABC的引用(内部某种指针也是AFAIK)
Not exactly.
Pointers are objects (variables) that require some storage and hold in that storage the address in memory of another object. References are pure aliases, like alternative names. In theory, they do not require any storage at all.
指针是需要一些存储的对象(变量),并在该存储中保存另一个对象的存储器中的地址。引用是纯别名,如替代名称。从理论上讲,它们根本不需要任何存储空间。
Per Paragraph 8.3.2/4 of the C++11 Standard:
根据C ++ 11标准的第8.3.2 / 4段:
It is unspecified whether or not a reference requires storage (3.7).
未指定引用是否需要存储(3.7)。
So a pointer to a reference is actually a pointer to the referenced object, and any operation done on a reference (apart from the act of binding it to an object upon initialization) is actually done on the object for which the reference is an alias.
因此,指向引用的指针实际上是指向引用对象的指针,并且对引用执行的任何操作(除了在初始化时将其绑定到对象的行为)实际上是在引用为别名的对象上完成的。
#2
1
I am struggling with the last sentence of your question ("a pointer to the adress of the reference"?)
我正在努力解决你问题的最后一句话(“指向参考地址的指针”?)
What can be said is that modifyABC()
takes a reference to an ABC
, and returns exactly the same reference. No copy of the object is made.
可以说是modifyABC()接受对ABC的引用,并返回完全相同的引用。没有对象的副本。
The overall effect of your code is that the address of foobar
is appended to ABCList
.
代码的整体效果是foobar的地址被附加到ABCList。
#3
1
Does the "adress of" & operator now return a pointer to the adress of the reference or the actually object behind the reference?
“地址”运算符现在是否返回指向引用地址或引用后面的实际对象的指针?
In C++, references, as such, do not have their own addresses. So address of a reference means address of the object the reference is referring to.
在C ++中,引用本身没有自己的地址。因此,引用的地址意味着引用所引用的对象的地址。
X x;
X &r = x; //reference
X *p = &r; //same as &x
Hope that helps.
希望有所帮助。
#4
1
Anything you do with a reference (including taking its address) is the equivalent of doing it to the referred to object. In C++, a reference itself is not an object, does not necessarily occupy space in memory, and does not have an address.
您使用引用执行的任何操作(包括获取其地址)都相当于对引用的对象执行操作。在C ++中,引用本身不是对象,不一定占用内存中的空间,也没有地址。
#5
0
A reference and a pointer are two different concepts. You may think of a reference as an alias to an existing object. So just like an alias to an alias is again alias of the original, here the return value of modifyABC() is again a reference to the original object. Taking the pointer to a reference always return the address of the object you have a reference of.
引用和指针是两个不同的概念。您可以将引用视为现有对象的别名。因此,就像别名的别名再次是原始的别名一样,这里的modifyABC()的返回值也是对原始对象的引用。将指针指向引用始终返回您引用的对象的地址。
#1
1
modifyABC()
returns a reference toABC
(which is internally some sort of pointer too AFAIK)modifyABC()返回对ABC的引用(内部某种指针也是AFAIK)
Not exactly.
Pointers are objects (variables) that require some storage and hold in that storage the address in memory of another object. References are pure aliases, like alternative names. In theory, they do not require any storage at all.
指针是需要一些存储的对象(变量),并在该存储中保存另一个对象的存储器中的地址。引用是纯别名,如替代名称。从理论上讲,它们根本不需要任何存储空间。
Per Paragraph 8.3.2/4 of the C++11 Standard:
根据C ++ 11标准的第8.3.2 / 4段:
It is unspecified whether or not a reference requires storage (3.7).
未指定引用是否需要存储(3.7)。
So a pointer to a reference is actually a pointer to the referenced object, and any operation done on a reference (apart from the act of binding it to an object upon initialization) is actually done on the object for which the reference is an alias.
因此,指向引用的指针实际上是指向引用对象的指针,并且对引用执行的任何操作(除了在初始化时将其绑定到对象的行为)实际上是在引用为别名的对象上完成的。
#2
1
I am struggling with the last sentence of your question ("a pointer to the adress of the reference"?)
我正在努力解决你问题的最后一句话(“指向参考地址的指针”?)
What can be said is that modifyABC()
takes a reference to an ABC
, and returns exactly the same reference. No copy of the object is made.
可以说是modifyABC()接受对ABC的引用,并返回完全相同的引用。没有对象的副本。
The overall effect of your code is that the address of foobar
is appended to ABCList
.
代码的整体效果是foobar的地址被附加到ABCList。
#3
1
Does the "adress of" & operator now return a pointer to the adress of the reference or the actually object behind the reference?
“地址”运算符现在是否返回指向引用地址或引用后面的实际对象的指针?
In C++, references, as such, do not have their own addresses. So address of a reference means address of the object the reference is referring to.
在C ++中,引用本身没有自己的地址。因此,引用的地址意味着引用所引用的对象的地址。
X x;
X &r = x; //reference
X *p = &r; //same as &x
Hope that helps.
希望有所帮助。
#4
1
Anything you do with a reference (including taking its address) is the equivalent of doing it to the referred to object. In C++, a reference itself is not an object, does not necessarily occupy space in memory, and does not have an address.
您使用引用执行的任何操作(包括获取其地址)都相当于对引用的对象执行操作。在C ++中,引用本身不是对象,不一定占用内存中的空间,也没有地址。
#5
0
A reference and a pointer are two different concepts. You may think of a reference as an alias to an existing object. So just like an alias to an alias is again alias of the original, here the return value of modifyABC() is again a reference to the original object. Taking the pointer to a reference always return the address of the object you have a reference of.
引用和指针是两个不同的概念。您可以将引用视为现有对象的别名。因此,就像别名的别名再次是原始的别名一样,这里的modifyABC()的返回值也是对原始对象的引用。将指针指向引用始终返回您引用的对象的地址。