I have a question regarding some kind of Null Object Pattern. When I think of a getter (I know that we should avoid that but suppose) I see 2 ways of doing that.
Suppose that we have a class NullObject.cpp
1)
class NullObject
{
std::vector<SomeObject> get() { return {}; }
}
class SomeImplementation
{
std::vector<SomeObject> get() { return someVectorMember; }
{
const std::vector<SomeObject>& object = instance.get();
So in first example we will always returning by value and assign to const Object&
2)
class NullObject
{
const std::vector<SomeObject>& get() { return member; }
static std::vector<SomeObject> member;
}
class SomeImplementation
{
const std::vector<SomeObject>& get() { return someVectorMember; }
{
const std::vector<SomeObject>& object = instance.get();
In this case we have static member in Null class so we can return a const reference.
Question: Which is better in case of performance for example? Which is better in case of "clean" code? Is there any (better) option? Maybe my example are wrong?
Thanks
Aucun commentaire:
Enregistrer un commentaire