logoalt Hacker News

ninkendo • today at 4:12 PM • 2 replies • view on HN

I don't see the link between constructors and whether or not built-in types have special treatment. Care to enlighten the class?


Replies

quietbritishjim • today at 4:19 PM

In C++ you can write "int x;" and get an object of type int.

If the only way to initialise a class-type object was to call some custom static method on it then you'd have to write "Foo x = Foo::create();". Suddenly it looks more special than a built-in type.

➕ show 2 replies
William_BB • today at 4:43 PM

If my understanding is correct, C++ built-in types can be declared without initializing them:

  int x;   // Unitialized, garbage value
  int x{}; // Value-initialized (falls back to zero-initialization)
We want classes to have the same behaviour:

  Struct s;   // Default-initializes (presumably we want members uninitialized)
  Struct s{}; // Value-initializes (presumably we want members initialized)
➕ show 1 reply