The meaning of Java-like default constructor in C++ [duplicate] - c++

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 5 years ago.
Suppose I have the following structure:
struct A {
A() { cout << "Default ctor\n"; }
A(int) { cout << "Ctor with params\n"; }
};
And then I want to create an object. In Java I've got accustomed to use brackets when I create an object, so the first desire is to write something like that:
A a();
The code compiles, but a actually isn't an instance of A, it is something different.
So the question is: what is a and why should I omit the brackets to call the default constructor?

See the most vexing parse, what you are actually doing is declaring a function. The way to alleviate this problem is to either eliminate the () or to use the C++11 uniform initialization syntax,
A a;
A a{};

In C++, A a(); is a forward declaration for a function called a that takes no arguments, and returns an A. It does not create an instance of A using the default constructor.
In Java, there is no need for forward declarations of functions, so A a(); can be read to be equivalent to A a;
This curiousity of C++ even has a name: see https://en.wikipedia.org/wiki/Most_vexing_parse

Related

constructor resolution, default constructors and () [duplicate]

This question already has answers here:
My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it?
(5 answers)
Closed 5 years ago.
#include <iostream>
using namespace std;
class A{
public:
A(){
cout << "Class A!";}
};
int main()
{
A a();
}
the above code does not call the constructor A :: A() even though they have the same input parameters (none). however if, in the main function, I remove the parenthesis from
A a();
it calls the constructor.
so what is the difference between A a; and A a();
i believe the question here is very similar maybe even the same however if some could explain in simpler terms I would be very grateful.
Do the parentheses after the type name make a difference with new?
would declaring A a(); ever call a constructor, under any circumstances?
do parameterless constructors exist in c++, or is that the same as a default constructor?
With
A a();
you declare a as a function taking no arguments and returning an A object.

c++ nested constructors call issue [duplicate]

This question already has answers here:
My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it?
(5 answers)
Closed 5 years ago.
I have 2 test classes:
class B {
public:
B(int i) {
qDebug() << "B constructor ";
}
};
class A {
public:
A(B b) {
qDebug() << "A constructor ";
}
};
Now i want to create A object with B, thats what i do:
int i = 5;
A test (B(i)); //does not work
Code compiles without errors, but second line doesnt execute at all. I've made some tests, and code below works well:
int i = 5;
A test (B((int)i)); //works
A test (B(5)); //works
So, i guess compiler cant interpret 'i' as int inside B constructor call, but why?
This:
A test(B(i));
is the same as the:
A test(B i);
which is a function declaration, not a call to constructor due to a most vexing parse.
There is a rule that states (S. Meyers, "Effective Modern C++"):
anything that can be parsed as a declaration must be interpreted as
one
To avoid that use braced initialization (instead of parentheses ()) as functions can't be declared with {} braces:
A test{B(i)};
That being said there are no "nested constructors calls" in your example.

Initializing a variable using default constructor [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 7 years ago.
I'm pretty new to c++, i'm now trying to learn all the basics,
I know when default constructors are called, but when i tried different syntax it doesn't work like i expected.
Look at the following code:
class a;
class b();
class c(NULL);
'class' is a class i created with default constructor,
for a and c everything works well, but for b it just won't recognize the variable as a class member.
As i see it b and c are basically the same, what's wrong than?
Thanks!
Don't name your class "class", as it is a reserved name.
As for C++, if the constructor takes no parameters, you instantiate it using
Foo a; // note, if you are using c++11, you can do Foo a{};
As opposed to:
Foo b();
Which actually does something totally unexpected*, and declares a function named b that returns a Foo instance.
As for Foo c(null), it won't compile as there is no default constructor that takes an argument.
* It is referred to as "the most vexing parse", though I find that to be an exaggeration. It can certainly catch you by surprise, but just knowing that you can declare a function prototype inside a function, should be enough to remove the "vexing" aspect.
In other words int getMyInt(); is obviously a function prototype when placed outside any function definitions. However, since this is also the case when inside a function definition, int getMyInt(); doesn't do anything it wouldn't normally do... which is to define a function prototype getMyInt that returns an integer.
b is interpreted as a declaration of a function taking no arguments and returning an object of type class.
This is known as the most vexing parse. Edit: This is not the most vexing parse.

What's the difference in instantiating variable with vs. without braces in C++? [duplicate]

This question already has answers here:
When to use the brace-enclosed initializer?
(3 answers)
Instantiate class with or without parentheses? [duplicate]
(1 answer)
Closed 9 years ago.
#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:
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.

Instance of class and function return type confusion [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 10 years ago.
I cannot do this:
class A
{
public:
A()
{
}
};
A a1();
Because A a1(); looks like a function prototype.
But I can do this:
class B
{
public:
B(std::string argument)
{
std::cout << argument;
}
};
B b1("Text");
These two things are essentially the same except the compiler is able to distinguish B b1("Text"); as NOT being a function prototype, because some data is passed in the parenthesis.
Is there any reason why the brackets must be omitted for A, or is the reason because the compiler thinks it is a function definition?
That's exactly it, and it's known as most vexing parse. The reason is that if A a1(); was treated as an object declaration, you wouldn't be able to declare a function with that prototype. And you want to be able to declare a function, right?
B b1("Text"); works because it can't be treated as a function prototype, but, for example, B b(A()); can and will.