Prev: How to test if a iterator is valid or not?
Next: return value optimization vs. returning a boost::shared_ptr of container
From: Dragan Milenkovic on 19 Jun 2010 17:49
> On 2010-06-17 15:37, Hakusa(a)gmail.com wrote:
>> On Jun 16, 9:56 am, DeMarcus<use_my_alias_h...(a)hotmail.com> wrote:
>>> As Daniel Kr�gler guessed, I'm not allowed to change the constructor of
>>> SomeObject, and even if I could, the number of permutations would be
>> What about applying a factory pattern? Pseudo-code:
>> SomeObFact factory;
>> factory.set_value( x );
>> factory.increase_rate( y );
>> SomeObject obj = factory.make();
>> Alternately, i wonder if you could make a child of SomeObject that
>> accepts the factory for its constructor.
> In this particular case I think a factory will not work, however, I've
> been considering using some helper class of some kind, so using a
> factory is not far from a good solution.
>>> A function may seem the obvious choice for most here, but to be picky,
>>> will I not get a slight performance hit compared to derivation since I
>>> have to return the object? Or will the function inlining take care of
>> If you're THAT worried, maybe my factory suggestion wouldn't be
>> optimal... Still thought i'd put it out there.
> Well, honestly, I usually don't care that much about performance. I
> rather get the design right. But in this particular case we had two
> almost identical solutions; derivation and function wrapping. Then I
> feel that it could be a good idea to have a look at which one performs
This derivation feels wrong (luckily I'm not to arrogant,
otherwise I would call it evil). These are not identical solutions.
The function would be a natural way to say "I am going to create
SomeObject with the given parameters". Making a new type doesn't
_really_ say that, does it?
If I'm not mistaken, a "factory" is a name for anything with
a purpose of creating objects. There can however be different
kinds of factories. Your helper class would also be a factory,
unless you had some other use in mind. So, either a factory
or a function (which IMHO is also a factory) will be
a good design. Unlike derivation.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]