0
#include <iostream>
using namespace std;

struct CTest 
{
    CTest() { cout << "Constructor called"; }
    CTest(string s) { cout << "Any constructor with parameters"; } 
};

int main () {
  CTest t1;
  CTest t2{};
}

I come from the Java world and there t1 would just have been declared which definitely isn't the case here since both both lines call the constructor of CTtest. In this case, t1 calls the overwritten default constructor as well as t2. Are there any cases where it actually makes a difference or can we always omit the braces?

Maybe it's just me, but I couldn't find any hint on that. There are only discussions about when to use braces vs. parentheses (vs. value vs. copy constructor).

fytwim
  • 1
  • 2
  • Parenthesis refers to `()`. Braces refers to `{}`. Brackets refers to `[]`. I don't see you using parenthesis anywhere... – apnorton Jan 08 '14 at 03:22
  • Those are braces, and I'm sure it's on here somewhere, but value-initialization vs. default-initialization. – chris Jan 08 '14 at 03:22
  • [This](http://stackoverflow.com/q/9976927/183120) and [this](http://stackoverflow.com/q/18222926/183120) are dupes for brace initialization, pick one :) – legends2k Jan 08 '14 at 03:24
  • There are quite a few subtly different types of initialization in C++, which can be confusing to someone new to the language. [This article](http://www.informit.com/articles/article.aspx?p=1852519) provides a good overview. – bcrist Jan 08 '14 at 03:28
  • @bcrist, I don't like that they use the term "default initialization" to mean value-initialization because there's already a specific default-initialization, and that doesn't do it. – chris Jan 08 '14 at 03:34
  • @anorton, I've grown up with brackets meaning () and the term "parentheses" not used much. We say "square brackets" for [], and I see/hear curly braces/brackets for {} often enough, but I prefer just braces. – chris Jan 08 '14 at 03:38
  • @chris hmm, you're right; they're saying `x{}` is default initialization, which it is not... I skimmed that part when I read it the first time, guess I should have read it closer. – bcrist Jan 08 '14 at 03:42
  • @bcrist, I think it's a made up term. They describe it as value-initialization, but don't use the proper term. The main problem is that the term they do use only differs from a completely different type of initialization by a hyphen. – chris Jan 08 '14 at 03:50
  • My question seems to have been misunderstood, so I edited it. To make it clear: I'm aware of the topics about when it's best to use braces or parentheses. I just want to know if there is a difference if we just leave the braces away. (if it doesn't make a difference here but in another example, I'd be interested in it too) – fytwim Jan 08 '14 at 20:21

1 Answers1

0

When the only constructor for a class is its default constructor then doing initialization with curly braces doesn't matter:

CTest t1;
CTest t2{};

Are the same.

Its only once you have other constructors that take parameters that putting values for those parameters inside of {} that you are doing something new.

woolstar
  • 5,063
  • 20
  • 31
  • Sorry to say, but they aren't. A defaulted or deleted default constructor will cause zero-initialization. Default-initialization is then done afterward if that constructor is non-trivial. – chris Jan 08 '14 at 03:27
  • Keep in mind there is a difference between the "trivial" default constructor and a user-provided one. – Jesse Good Jan 08 '14 at 03:32
  • @JesseGood, And even a non-user-provided one can be non-trivial. Consider a class containing a member with a non-trivial default constructor (full list in § 12.1 /5). – chris Jan 08 '14 at 03:39