Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use virtual 'Initialize()' functions to initialize an object of my class?

I'm currently having a discussion with my teacher about class design and we came to the point of Initialize() functions, which he heavily promotes. Example:

class Foo{
public:
  Foo()
  { // acquire light-weight resources only / default initialize
  }

  virtual void Initialize()
  { // do allocation, acquire heavy-weight resources, load data from disk
  }

  // optionally provide a Destroy() function
  // virtual void Destroy(){ /*...*/ }
};

Everything with optional parameters of course.

Now, he also puts emphasis on extendability and usage in class hierarchies (he's a game developer and his company sells a game engine), with the following arguments (taken verbatim, only translated):

Arguments against constructors:

  • can't be overridden by derived classes
  • can't call virtual functions

Arguments for Initialize() functions:

  • derived class can completely replace initialization code
  • derived class can do the base class initialization at any time during its own initialization

I have always been taught to do the real initialization directly in the constructor and to not provide such Initialize() functions. That said, I for sure don't have as much experience as he does when it comes to deploying a library / engine, so I thought I'd ask at good ol' SO.

So, what exactly are the arguments for and against such Initialize() functions? Does it depend on the environment where it should be used? If yes, please provide reasonings for library / engine developers or, if you can, even game developer in general.


Edit: I should have mentioned, that such classes will be used as member variables in other classes only, as anything else wouldn't make sense for them. Sorry.

like image 659
Xeo Avatar asked Jun 24 '11 16:06

Xeo


3 Answers

For Initialize: exactly what your teacher says, but in well-designed code you'll probably never need it.

Against: non-standard, may defeat the purpose of a constructor if used spuriously. More importantly: client needs to remember to call Initialize. So, either instances will be in an inconsistent state upon construction, or they need lots of extra bookkeeping to prevent client code from calling anything else:

void Foo::im_a_method()
{
    if (!fully_initialized)
        throw Unitialized("Foo::im_a_method called before Initialize");
    // do actual work
}

The only way to prevent this kind of code is to start using factory functions. So, if you use Initialize in every class, you'll need a factory for every hierarchy.

In other words: don't do this if it's not necessary; always check if the code can be redesigned in terms of standard constructs. And certainly don't add a public Destroy member, that's the destructor's task. Destructors can (and in inheritance situations, must) be virtual anyway.

like image 109
Fred Foo Avatar answered Oct 19 '22 00:10

Fred Foo


I"m against 'double initialization' in C++ whatsoever.

Arguments against constructors:

  • can't be overridden by derived classes
  • can't call virtual functions

If you have to write such code, it means your design is wrong (e.g. MFC). Design your base class so all the necessary information that can be overridden is passed through the parameters of its constructor, so the derived class can override it like this:

Derived::Derived() : Base(GetSomeParameter()) 
{
}
like image 41
Yakov Galka Avatar answered Oct 19 '22 01:10

Yakov Galka


This is a terrible, terrible idea. Ask yourself- what's the point of the constructor if you just have to call Initialize() later? If the derived class wants to override the base class, then don't derive.

When the constructor finishes, it should make sense to use the object. If it doesn't, you've done it wrong.

like image 21
Puppy Avatar answered Oct 19 '22 00:10

Puppy