most-vexing-parse

Why does this call the default constructor?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 06:09:48
struct X { X() { std::cout << "X()\n"; } X(int) { std::cout << "X(int)\n"; } }; const int answer = 42; int main() { X(answer); } I would have expected this to print either X(int) , because X(answer); could be interpreted as a cast from int to X , or nothing at all, because X(answer); could be interpreted as the declaration of a variable. However, it prints X() , and I have no idea why X(answer); would call the default constructor. BONUS POINTS: What would I have to change to get a temporary instead of a variable declaration? nothing at all, because X(answer); could be interpreted as the

Most vexing parse [duplicate]

佐手、 提交于 2019-11-27 02:11:52
This question already has an answer here: A confusing detail about the Most Vexing Parse 4 answers I saw a code here at Cpp Quiz [Question #38] #include <iostream> struct Foo { Foo(int d) : x(d) {} int x; }; int main() { double x = 3.14; Foo f( int(x) ); std::cout << f.x << std::endl; return 0; } It is said there that this code is ill formed because Foo f( int(x) ); will be treated as a function declaration rather than an object declaration of type Foo . As far as I know, this is an instance of most vexing parse. My question is what is this syntax int(x) in statement Foo f( int(x) ); means? So

Difference between creating object with () or without

前提是你 提交于 2019-11-26 15:38:43
问题 i just run into the problem error: request for member ‘show’ in ‘myWindow’, which is of non-class type ‘MainGUIWindow()’ when trying to compile a simple qt-application: #include <QApplication> #include "gui/MainGUIWindow.h" int main( int argc, char** argv ) { QApplication app( argc, argv ); MainGUIWindow myWindow(); myWindow.show(); return app.exec(); } I solved this by replacing MainGUIWindow myWindow(); by MainGUIWindow myWindow; but I don't understand the difference. My question: What is

Why does C++ allow us to surround the variable name in parentheses when declaring a variable?

。_饼干妹妹 提交于 2019-11-26 10:35:00
For example a declaration such as that: int (x) = 0; Or even that: int (((x))) = 0; I stumbled upon this because in my code I happened to have a fragment similar to the following one: struct B { }; struct C { C (B *) {} void f () {}; }; int main() { B *y; C (y); } Obviously I wanted to construct object C which then would do something useful in its destructor. However as it happens compiler treats C (y); as a declaration of variable y with type C and thus it prints an error about y redefinition. Interesting thing is that if I write it as C (y).f () or as something like C (static_cast<B*> (y))

constructor invocation mechanism

 ̄綄美尐妖づ 提交于 2019-11-26 09:52:43
问题 struct my { my(){ std::cout<<\"Default\";} my(const my& m){ std::cout<<\"Copy\";} ~my(){ std::cout<<\"Destructor\";} }; int main() { my m(); //1 my n(my()); //2 } Expected output : 1 ) Default 2 ) Copy Actual output : What\'s wrong with my understanding of the constructor invoking mechanism? Note I have omitted header files for brevity. 回答1: Case 1) m is interpreted as a function return my and taking no arguments. To see the expected output remove () i.e use my m; Case 2) This is something

Most vexing parse [duplicate]

隐身守侯 提交于 2019-11-26 09:04:15
问题 This question already has an answer here: A confusing detail about the Most Vexing Parse 4 answers I saw a code here at Cpp Quiz [Question #38] #include <iostream> struct Foo { Foo(int d) : x(d) {} int x; }; int main() { double x = 3.14; Foo f( int(x) ); std::cout << f.x << std::endl; return 0; } It is said there that this code is ill formed because Foo f( int(x) ); will be treated as a function declaration rather than an object declaration of type Foo . As far as I know, this is an instance

Why does C++ allow us to surround the variable name in parentheses when declaring a variable?

戏子无情 提交于 2019-11-26 02:11:56
问题 For example a declaration such as that: int (x) = 0; Or even that: int (((x))) = 0; I stumbled upon this because in my code I happened to have a fragment similar to the following one: struct B { }; struct C { C (B *) {} void f () {}; }; int main() { B *y; C (y); } Obviously I wanted to construct object C which then would do something useful in its destructor. However as it happens compiler treats C (y); as a declaration of variable y with type C and thus it prints an error about y

A confusing detail about the Most Vexing Parse

五迷三道 提交于 2019-11-26 00:44:50
问题 My question is how the following line can be parsed as a function declaration: vector<int> v(istream_iterator<int>(cin), istream_iterator<int>()); I understand most of the details of the Most Vexing Parse and why the second temporary iterator can be interpreted as a type that is a function returning an iterator and taking no arguments, but what I don\'t get is why the first temporary iterator can be interpreted as a type. What type does it represent? My thought is that it would be some sort

Why is there no call to the constructor? [duplicate]

点点圈 提交于 2019-11-25 23:18:55
问题 This question already has an answer here: Default constructor with empty brackets 9 answers This code doesn\'t behave how I expect it to. #include<iostream> using namespace std; class Class { Class() { cout<<\"default constructor called\"; } ~Class() { cout<<\"destrutor called\"; } }; int main() { Class object(); } I expected the output \'default constructor called\', but I did not see anything as the output. What is the problem? 回答1: Nope. Your line Class object(); Declared a function. What

A confusing detail about the Most Vexing Parse

旧巷老猫 提交于 2019-11-25 22:49:35
My question is how the following line can be parsed as a function declaration: vector<int> v(istream_iterator<int>(cin), istream_iterator<int>()); I understand most of the details of the Most Vexing Parse and why the second temporary iterator can be interpreted as a type that is a function returning an iterator and taking no arguments, but what I don't get is why the first temporary iterator can be interpreted as a type. What type does it represent? My thought is that it would be some sort of function type, but I can't see how the name cin gets used. Is it declaring that the parameter is an