question on NVR optimization

C

cai

Hello everyone, I am reading Inside the c++ object model(Lippman). In
section 2.3 of Chapter 2, I am puzzled by an example.
class test
{
friend test foo(double);
public:
test()
{memset(array,0,100*sizeof(double));}
private:
double array[100];
}
test foo(double val)
{
test local;
local.array[0]=val;
local.array[99]=val;
return local;
}
main()
{
for(int cnt=0;cnt<1000000;cnt++)
{ test t=foo(double(cnt)); }
return 0;
}
Lippman says that this example couldn't optimize NVR because test class
hasn't a copy constructor. If adding an inline copy constructor like:
inline test::test(const test& t)
{
memcpy(this,&t,sizeof(test));
}
Then NVR is enabled.
But I CAN'T really understand why copy constructor has effect on NVR
optimization, and why it can enable this!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top