KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
i'm trying to come up with a concrete reasoning why to use a pointer over a reference as a return type from a function , my reasoning is that if inadvertently a null value is returned to a reference type it could not be checked , and could lead to run-time errors int& something(int j) { int* p = 0 ; return *p; } void main() { int& i = something(5); i = 7; // run-time error } if i had used a pointer i could check it and avoid the error the pointer return value would act as a contract to that a value must be returned. void main() { int* i = something(5); if( i != null ) *i = 7; } any thoughts would be appreciated again , what would you use and why reference or pointer thanks in advance.
Tags (comma-separated)
Save Edits
Cancel