Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am confused on this functionx tutorial....
<a href="http://www.functionx.com/cpp/examples/returnpointer.htm">http://www.functionx.com/cpp/examples/returnpointer.htm</a>[<a href="http://www.functionx.com/cpp/examples/returnpointer.htm" target="_blank" title="New Window">^</a>]
will returning a pointer to an local object of myclass is correct? will that object destroyed on going out of function scope... confused....
I raised the question in msdn forum... still not clear...

http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/75f14e6f-7e75-4d3a-abc0-b18514eed24c/?prof=required[^]
Posted

I'm not sure what is unclear exactly... You seem to have gotten it right - returning pointers to local data is not safe and should be avoided. Nothing more to add really.

EDIT: To answer your second question:
IMHO the tutorial is wrong.
The copy constructor of the class will not be called.

By the way - you should not post additional questions as 'answers' - use the 'Add comment' link under existing answers to ask additional questions or comment on the answers others give you. This keeps the thread nice and clear for everyone to view :)
 
Share this answer
 
v2
Comments
Hans Dietrich 2-May-11 1:51am    
Correct, my 5.
Olivier Levrey 2-May-11 8:15am    
Agree my 5. The rule is simple: never return the address of a local variable.
Hi,

Your tutorial is junk, don't trust it anywhere else.

This, and other similar changes would be correct:
double * GetSalary()
{
    static double salary = 26.48;

    return &salary;
}

cheers,
AR
 
Share this answer
 
Comments
Olivier Levrey 2-May-11 8:16am    
Agree. 5.
The tutorial is wrong: never return the address of a local object.
:-)
 
Share this answer
 
Comments
Hans Dietrich 2-May-11 4:56am    
Unless it's a static, like Alain says.
Olivier Levrey 2-May-11 8:17am    
Agree. 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900