Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing member variables

Tags:

c++

I've started to pick up this pattern:

template<typename T>
struct DefaultInitialize
{
   DefaultInitialize():m_value(T()){}
   // ... conversions, assignments, etc ....
};

So that when I have classes with primitive members, I can set them to be initialized to 0 on construction:

struct Class
{
  ...
  DefaultInitialize<double> m_double;
  ...
};

The reason I do this is to avoid having to remember to initialize the member in each constructor (if there are multiple constructors). I'm trying to figure out if:

  • This is a valid pattern?
  • I am using the right terminology?

2 Answers

This is a valid pattern?

It's a known "valid" pattern, i would say. Boost has a class template called value_initialized that does exactly that, too.

I am using the right terminology?

Well, your template can be optimized to have fewer requirements on the type parameter. As of now, your type T requires a copy constructor, unfortunately. Let's change the initializer to the following

DefaultInitialize():m_value(){}

Then, technically this kind of initialization is called value initialization, starting with C++03. It's a little bit weird, since no kind of value is provided in the first place. Well, this kind of initialization looks like default initialization, but is intended to fill things with zero, but respecting any user defined constructor and executing that instead.

To summarize, what you did was to value initialize an object having type T, then to copy that object to m_value. What my version of above does it to value initialize the member directly.

like image 58
Johannes Schaub - litb Avatar answered Sep 15 '26 17:09

Johannes Schaub - litb


Seems like a lot of work to avoid having to type m_double(0). I think it's harder to understand at first glance, but it does seem fine as long as everything is implemented properly.

But is it worth it? Do you really want to have to #include "DefaultInitialize.h" everywhere?


To clarify, basically, you're:

  • Making your compile times longer because of the includes.
  • Your code base larger because you have to manage the deceptively simple DefaultInitialize class
  • Increase the time it takes other people to read your code. If you have a member of a class that's a double, that's natural to me, but when I see DefaultInitialize, I have to learn what that is and why it was created

All that because you don't like to type out a constructor. I understand that it seems very nice to not have to do this, but most worth-while classes I've ever written tend to need to have a constructor written anyway.

This is certainly only my opinion, but I think most other people will agree with it. That is: it would be handy to not have to explicitly initialize members to 0, but the alternative (your class) isn't worth it.

Not to mention that in C++0x, you can do this;

class Foo
{
private:
    int i = 0; // will be initialized to 0
}
like image 29
GManNickG Avatar answered Sep 15 '26 17:09

GManNickG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!