Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 2
    Also I remember cases where it was simply not possible to go from the constructor, but I don't have an example to show right now Commented Apr 14, 2016 at 12:23
  • 2
    The example now describes a factory pattern - but the first sentence actually describes RAII paradigm. So which one is this answer supposed to recommend? Commented Apr 14, 2016 at 12:48
  • 11
    @Ext3h I don't think the factory pattern and RAII are mutually exclusive. Commented Apr 14, 2016 at 12:57
  • @PieterB No, they are not, a factory may ensure RAII. But only with the additional implication that the template/constructor args (called UnitializedFooManager) must not share a state with the instances (InitializedFooManager), as Initialize(...) has actually Instantiate(...) semantic. Otherwise, this example leads to new problems if the same template is used twice by a developer. And that's not possible to prevent/validate statically either if the language doesn't explicitly support move semantics in order to reliably consume the template. Commented Apr 14, 2016 at 13:19
  • 2
    @Ext3h: I recommend the first solution since it is the simplest. But if the client need to be able to access the object before calling Initialize, then you can't use it, but you might be able to use the second solution. Commented Apr 14, 2016 at 14:35