c++ - Pointer vs Return -


which faster when assigning variable via method, return variable, or point variable?

case 1:

function declaration

void foo(int* number) {     *number = 5; } 

usage

int main() {     int number;     function(&number);     cout << "number: " << number; } 

case 2:

function declaration

int foo() {     int number = 5;     return number; } 

usage

int main() {     int number;     number = function();     cout << "number: " << number; } 

ps: in case 2, created variable , returned instantly. know doesn't make sense, closest example can find situation i'm dealing with, since i'm initializing actual object, requires creating object first, editing it, returning it

it depends on cost of copying variable. primitive types, return value. more complex types consider passing in reference, or take @ c++11 move semantics.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -