0

In my code i have ints, bools, pointers and so on, i also have some type defined by me with typedef, how can i manage the default value initialization like it happens in the objects with the contrunctor?

I want to be sure that

T var;

if untouched, is always equal to my default value and i like to do this without parsing each line of code anche changing the default value manually and without using a preprocessor macro.

is this possible? for a new typedef is possible to define a default value?

user827992
  • 1,743
  • 13
  • 25
  • 1
    A typedef doesn't create a new type, it just creates an alias for an existing type. – Oliver Charlesworth Jul 15 '12 at 16:43
  • Okay, I'll bite. *Why* don't you want to use a constant/preprocessor macro? This is the exact use case they were designed for. – Jonathan Grynspan Jul 15 '12 at 16:46
  • @JonathanGrynspan it's basically a problem about the nature and the feature of this macros, if i wrote `#define N 5` somewhere i can't be sure that `N = 5` in my code because this can be easily overwritten. also take a look at http://stackoverflow.com/questions/473354/quote-needed-preprocessor-usage-is-bad-oo-practice – user827992 Jul 15 '12 at 16:52
  • if `T` is your type just add that in the constructor, otherwise you can use wrapping like Xeo said.... – ted Jul 15 '12 at 16:53
  • @ted my main interest is in the primitive types. – user827992 Jul 15 '12 at 16:56
  • If you hate the preprocessor that much, why not use a named constant, e.g. `const int MY_VALUE = 12345;`? – Jonathan Grynspan Jul 15 '12 at 16:57
  • @JonathanGrynspan i hate nothing, it's just the fact that it does not offer a good feature for some critical aspect like the initialization. the const is just another type, i can't use a type to solve a specific issue that i have with another type, this is a dirty solution. – user827992 Jul 15 '12 at 17:00
  • A constant isn't a type, it's a value, and it's the appropriate solution to your problem. Nobody's going to go around changing your initializations behind your back. – Jonathan Grynspan Jul 15 '12 at 17:05

3 Answers3

6

In C++11, you could write T var{}; to get value initialization to the default value.

In C++03, you could write a non-POD wrapper, whose default constructor will get called by T var;:

template<class T>
struct default_initializer{
  default_initializer() : value() {}
  default_initializer(T const& v) : value(v) {}
  T value;
};

// somewhere in code
default_initializer<T> var; // calls default ctor and initializes 'value'
                            // to its default value

This will allow you to safely default initialize even primitive , POD and aggregate types, which are normally left uninitialized by the T var; declaration.

Xeo
  • 129,499
  • 52
  • 291
  • 397
3

This is not possible for primitive types since they don't have a constructor. primitive types which are declared in the context of a function scope are not initialized by default and contain garbage. primitive variables which are declared in the global scope as global variables are always initialized to 0.

shoosh
  • 76,898
  • 55
  • 205
  • 325
0

There is no way to achieve this for ints, bools, pointers and other primitive data types without having to write some additional code whenever you declare values of such types. But for instances of your custom classes, there is.

Desmond Hume
  • 8,037
  • 14
  • 65
  • 112