I
Immortal Nephi
I need to follow best practice guideline how to design my code
correctly. I have to decide which either global function or class can
be chosen.
I will use global function if global function is available to all
file scopes or is in namespace scope. I need to avoid data
modification when necessary.
I can write good global function.
int gFunc( int x, int y ) {
// Do something
// ….
return z;
}
If I have to modify either x or y in the function parameter, I will
put const in function parameter or create temporary variable.
int gFunc( const int x, const int y ) {
// Do something
// ….
return z;
}
// or…
int gFunc( int x, int y ) {
int temp_x = x;
int temp_y = y;
// Do something
// ….
return z;
}
If I want to put character buffer (like string) or class name in the
function parameter, I will need to put reference in it to avoid
overhead of copying each data size. Also, I will remember to put
const before reference if necessary.
class Object {
int x;
int y;
int z;
}
Object gFunc( const Object& obj ) {
Object temp_obj;
// Do something
// ….
return temp_obj;
}
The return type requires to create temporary object before all data
members in class Object is copied and temp_obj is destroyed from the
stack.
Tell me why you have reason to add reference to the return type.
class Data {
int x;
int y;
int z;
};
class Object {
Data m_Data;
Data &DoSomething() {
// Do something
return m_Data;
}
};
Notice DoSomething has reference return type. Do you need to use
reference return type? If yes, what is it used for?
Maybe, you want to suggest your code to explain why you need
reference return type. Reference return type is dangerous unless you
use return ‘this’ pointer.
correctly. I have to decide which either global function or class can
be chosen.
I will use global function if global function is available to all
file scopes or is in namespace scope. I need to avoid data
modification when necessary.
I can write good global function.
int gFunc( int x, int y ) {
// Do something
// ….
return z;
}
If I have to modify either x or y in the function parameter, I will
put const in function parameter or create temporary variable.
int gFunc( const int x, const int y ) {
// Do something
// ….
return z;
}
// or…
int gFunc( int x, int y ) {
int temp_x = x;
int temp_y = y;
// Do something
// ….
return z;
}
If I want to put character buffer (like string) or class name in the
function parameter, I will need to put reference in it to avoid
overhead of copying each data size. Also, I will remember to put
const before reference if necessary.
class Object {
int x;
int y;
int z;
}
Object gFunc( const Object& obj ) {
Object temp_obj;
// Do something
// ….
return temp_obj;
}
The return type requires to create temporary object before all data
members in class Object is copied and temp_obj is destroyed from the
stack.
Tell me why you have reason to add reference to the return type.
class Data {
int x;
int y;
int z;
};
class Object {
Data m_Data;
Data &DoSomething() {
// Do something
return m_Data;
}
};
Notice DoSomething has reference return type. Do you need to use
reference return type? If yes, what is it used for?
Maybe, you want to suggest your code to explain why you need
reference return type. Reference return type is dangerous unless you
use return ‘this’ pointer.