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.
Related
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 3 years ago.
If I have a following situation:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "Inside A" << endl;
}
};
int main() {
A a();
return 0;
}
Why is the constructor not invoked?
If something looks like a function declaration, the C++ standard requires it be treated as a function declaration.
A a(); does not default-construct an object a of type A. It declares a function a that takes no input parameters and returns an A object as output.
To default-construct a variable a, you need to drop the parenthesis:
A a;
Or, in C++11 and later, you can use curly braces instead of parenthesis:
A a{};
This question already has answers here:
Default constructor with empty brackets
(9 answers)
What does X f() mean?
(4 answers)
Closed 4 years ago.
Why there is no output?
Why doesn’t an ambiguity error occur?
#include <iostream>
using namespace std;
class A{
public:
A(){
cout << "Class A";
}
A(int a = 0){
cout << "A";
}
};
int main() {
A a();
return 0;
}
So when we create an object “a” of type “A” A a();, compiler calls a constructor without parameters, doesn’t he?
In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments. A constructor is called depending upon the number and type of arguments passed.
While creating the object, arguments must be passed to let compiler know, which constructor needs to be called.
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
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.
This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 7 years ago.
Consider the following code.
Here, A a(B()) compiles even though the constructor is A(B& b);
But print(B()) does not work. But print is also declared as print(B& b);
Why this inconsistency?
#include <iostream>
using namespace std;
class B{
public:
char b;
};
class A {
public:
B b;
A(B& b);
A() { }
};
A::A(B& b) {
this->b = b;
}
void print(B& b) { }
int main(){
print(B());
A a(B());
}
It compiles because it's not creating an instance of A. It's declaring a function named a that returns an A and receives one unnamed parameter of type pointer-to-function-returning-B. Since it's just a declaration, it compiles. If you're referred to a elsewhere in the code, you'd have seen additional problems. For why that's a function declaration instead of an object definition, the term to look up is most vexing parse.
This:
A a(B());
isn't doing what you think. It is actually parsed as a function declaration. This is commonly referred to as the "most vexing parse" in C++ (there are many posts here about it, if you search for that phrase).
You should not be passing a non-constant reference to a temporary, so the print statement should not compile. If you modify that reference in print to const, it will work.
Where you are trying to call the constructor you are actually declaring a function:
A a(B());
This declares a as a function returning A and taking as parameter a function pointer returning B and taking no parameters.
Actually trying to call the constructor results in an error, as expected:
A a = A(B());
tst.cpp:32: error: no matching function for call to ‘A::A(B)’