[英]When the return type is a class, what's the name of the pointer to the return value?
An example: 一个例子:
class A
{
....
A func();
};
A A::func()
{
...
return something;
}
At assembly level, when compiled, the function A::func
will actually have two parameters: the first one is the this
pointer, and the second one is the address of a temp A
object, created by the caller to store the return value. 在汇编级别,编译时,函数A::func
实际上将具有两个参数:第一个是this
指针,第二个是由调用方创建的用于存储返回值的temp A
对象的地址。
For example, if we write a.func()
, the program will create a temp A
object (let's call it t
) in the stack, and pass the address of a
as first parameter, the address of t
as second parameter, finally call the function func
. 例如,如果我们编写a.func()
,程序将在堆栈中创建一个temp A
对象(我们将其称为t
),并将a
的地址作为第一个参数传递, t
的地址作为第二个参数传递,最后调用函数func
。
Here is my question: in the realization of A::func
, we can get the address of a
- it's the pointer this
; 这里是我的问题:在实现A::func
,我们可以得到的地址a
-它的指针this
; but do we have a way to get the address of t
? 但是我们有办法得到t
的地址吗? What's its name? 它叫什么名字?
It will be useful to have it if, for example, I would like to do some memory alloc/free before returning the result. 例如,如果我想在返回结果之前进行一些内存分配/释放操作,则拥有它会很有用。
Here is an example of what I want to do: 这是我想做的一个例子:
class A
{
int * data;
A func();
};
A A::func()
{
// here "ret_p" is the pointer to the return value (let's pretend that it exists)
ret_p->data = new int[some length];
...
return * ret_p;
}
Of course, I can create a local object in A::func
and then return it; 当然,我可以在A::func
创建一个本地对象,然后将其返回。 but then the program will do a copy between my local object and the temp object created by the caller. 但是程序会在我的本地对象和调用者创建的临时对象之间进行复制。 Since the caller already created a temp object, I'm hoping that I can save both time and space by just using it directly. 由于调用者已经创建了一个临时对象,所以我希望直接使用它可以节省时间和空间。 Is that possible? 那可能吗?
Well this is maybe out of c++, but I'm still hoping... 好吧,这可能不是c ++,但我仍然希望...
There is no such a temp A
object parameter ( t
) in the stack. 堆栈中没有这样的temp A
对象参数( t
)。
If you call A a1 = a.func();
如果调用A a1 = a.func();
and return something;
并return something;
inside, the copy constructor
will be called, equivalent to this A a1(something);
在内部,将调用copy constructor
,等效于此A a1(something);
. 。 The a1
and something
are different instances. a1
和something
是不同的实例。
If you A a1; a1 = a.func();
如果您A a1; a1 = a.func();
A a1; a1 = a.func();
and return something;
并return something;
inside, the a1 = something; // (operator =)
在里面, a1 = something; // (operator =)
a1 = something; // (operator =)
will be called. a1 = something; // (operator =)
将被调用。 The a1
and something
are different instances. a1
和something
是不同的实例。
If you call A a1 = a.func();
如果调用A a1 = a.func();
and return A(p1);
并return A(p1);
inside, this is equivalent to A a1(p1);
在内部,这等效于A a1(p1);
, there is one instance a1
. ,有一个实例a1
。
If you call a.func();
如果调用a.func();
directly without assigning to a var and return something;
直接分配给var而不return something;
inside, nothing happens when return. 在内部,返回时什么也没有发生。
If you call a.func();
如果调用a.func();
directly without assigning to a var and return A(p1);
直接而不分配给var并return A(p1);
inside, a temp object will be constructed and then destroyed immediately. 在内部,将构造一个临时对象,然后立即销毁它。
If you A a1; a1 = a.func();
如果您A a1; a1 = a.func();
A a1; a1 = a.func();
and return A(p1);
并return A(p1);
inside, a temp object will be constructed, and then operator= will be called a1 = temp object;
在内部,将构造一个临时对象,然后将operator =称为a1 = temp object;
and then the temp object will be destroyed. 然后临时对象将被销毁。
For your reference. 供你参考。
After all, A a1 = a.func()
or A a1; a1 = a.func()
毕竟, A a1 = a.func()
或A a1; a1 = a.func()
A a1; a1 = a.func()
, return something; // a var
A a1; a1 = a.func()
, return something; // a var
return something; // a var
or return A(p1); // call constructor
return something; // a var
或return A(p1); // call constructor
return A(p1); // call constructor
will cause different behaviors, I think you can control memory correctly. return A(p1); // call constructor
会导致不同的行为,我认为您可以正确控制内存。
The supplying of &a
to func
is a form of copy elision . 向func
提供&a
是复制省略的一种形式。
There is no guarantee that will happen at all, and no C++ level access to the implementation. 根本无法保证会发生这种情况,也没有C ++级别的实现访问。
I don't think you're clear in your own head when you say "I would like to do some memory alloc/free before returning the result", can you give a specific example? 当您说“我想在返回结果之前先分配一些内存/释放内存”时,我认为您不清楚自己的想法,您能举一个具体的例子吗? Probably everything you need is made possible by copy elision, move operators, and RAII. 复制省略,移动运算符和RAII可能使您所需的一切成为可能。
Responding to your example, here's how you should do it. 响应您的示例,这是您应该如何做的。
class A
{
std :: vector <int> data;
A func();
};
A A::func()
{
A ret;
ret .data .assign (some_length, 123); // or whatever;
return ret;
}
This will probably optimise the way you want automatically due to copy elision. 由于复制省略,这可能会优化您想要的自动方式。 If you think the compiler won't elide the copy, add a move constructor. 如果您认为编译器不会删除副本,请添加move构造函数。
A :: A (A && old)
: data (std :: move (old .data))
{
}
std::vector
's move constructor will simply copy the pointer. std::vector
的move构造函数将只复制指针。 If you want to know how that works, well here's the homegrown equivalent. 如果你想知道如何工作的 ,以及这里的本土的同等学历。
class A
{
int * data;
// Allocate
A (size_t size) : data (new int [size]) {}
// Free, only if we haven't moved.
~ A () {if (data) delete [] data;}
// Move
A (A && old) : data (old .data) {old .data = nullptr;}
A (const A &) = delete; // or implement with a new allocation.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.