#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).
When the only constructor for a class is its default constructor then doing initialization with curly braces doesn't matter:
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.