Reputation: 39
Which one of those two methods should i use?
int Foo= XXX::instance().yyy(NULL);
OR
auto &q = XXX::instance();
int Foo = q.yyy(NULL);
Upvotes: 1
Views: 63
Reputation: 1
Using the form
auto &q = XXX::instance();
int Foo = q.yyy(NULL);
has at least two clear advantages:
It's shorter to write q.foo()
, instead of having to write XXX::instance().foo()
everywhere you need the instance
Supposed that XXX
is a Singleton, and that's generally considered a bad design (because you have a direct coupling to XXX
), that form would make it way easier to refactor the code later, as soon you want to replace XXX
with a more generic interface.
Upvotes: 3
Reputation: 1797
I would use
auto &q = XXX::instance();
int Foo = q.yyy(NULL);
for the following reasons:
q.
instead of repeating yourself.Upvotes: 0