Home >Backend Development >C++ >Should You Return Objects by Constant Value in C ?
Avoiding the Hazards of Returning by Constant Value
The practice of returning objects by constant value in C has been the subject of debate. This article explores the purpose behind this approach and examines the potential drawbacks.
Understanding the Const Qualifier
In the provided code example:
const Object myFunc() { return myObject; }
the const qualifier indicates that the returned object is constant. This means that the object cannot be modified after it is returned.
Arguments for Using Const
Advocates of returning by constant value argue that it:
However, as pointed out in the original question, these benefits can be achieved through other means, such as the explicit keyword for bool conversions.
Drawbacks of Returning by Const
Conclusion
While there may have been a rationale for returning by constant value in the past, it is no longer considered a recommended practice in modern C . Returning objects by non-const value offers more flexibility and is aligned with best practices involving rvalue references.
The above is the detailed content of Should You Return Objects by Constant Value in C ?. For more information, please follow other related articles on the PHP Chinese website!