What is the best practice to return references to class ways is that there is a case that the original type you want to return without reference , While the class items you want to return from context. Any article, best practice article that you recommend.
I think that by means of class system you mean member function. And by "returning by reference" you "return to the context of member data" means that it is primarily opposed to returning to the context of the local, which is clearly wrong.
When should you refresh the reference to member data, and when the data is itself?
By default, you should return the data (aka "according to the price") with this reference, avoids many problems:
-
Users will go to the false indicator to store this context and live throughout your members (your>
-
The user code is reliant on the exact return type . For example, for the implementation you have a
vector & lt; T & gt;
(and that is what your recipient's return) user code like "vector
" appears. You can then change your implementation (and greater) to use your code (T & gt; foo = obj.getItems () deck & lt; T & gt;
- user code breaks.) If you were returning from the price, you could easily You can create a local vector from a member, copy the data from the deck, and return the result. It is quite suitable for small size collection.
So when you want back instead of context?
- You can consider it When the returned object is too large (
image
) or non-duplication (boost signal). But, as usual, you should do your class instead Instead of content, you can opt for more OOP patterns by having hanging on in the
image
case, you can see theimage & amp;
Instead of creating a circle on it and its users can provide thedrawCircle
member function. / Li>- When your data is available Kik be owned by your user, and consider holding him to his STD collection:
vector & lt; T & gt; :: Operator []
returns a reference , because I have to do this: my exact object, not its copy - When your data is available Kik be owned by your user, and consider holding him to his STD collection:
[*] There is a better way to ensure future proof codes. Instead of returning a vector (by reference of value) a pair of a vector returns to your vector - a start and an ending is that it everything is usually a deck to your users Does with vector, but independent of the actual implementation. Boost offers boost :: iterator_pair
for this purpose. In the form of perk, the operator [] is overloaded, so that you " int i = obj.getItems () [5]
" instead of " int i = obj.getItems (tax Can also) (). Bebin () [5]
".
This solution is generalized for any condition that allows you to treat type in general. For example, if you have a dog
member but your users It is only necessary to know that it is a animal
(because they only eat ()
and sleep ()
), a freestore of your dog Return the animal reference / indicator in the allocated copy. So when you decide the dog you are weak and you really need a wolf to implement, the user code does not break.
This kind of information-hidden compatibility. It also helps to keep your design clean.
Comments
Post a Comment