Home > Backend Development > C++ > Is `widget w(gadget(), doodad());` a Variable Declaration in C ?

Is `widget w(gadget(), doodad());` a Variable Declaration in C ?

Linda Hamilton
Release: 2024-11-19 07:53:02
Original
908 people have browsed it

Is `widget w(gadget(), doodad());` a Variable Declaration in C  ?

C 's Most Vexing Parse Resolved

The issue arises with code like the following:

widget w(gadget(), doodad());  // Pitfall: Not a Variable Declaration
Copy after login

One might assume this code is declaring a variable named w of type widget. However, this is not the case.

In C , arguments of type array decay into pointers to the first element, and arguments of type function decay into a function pointer. This means the signature of the function being declared is:

widget w(gadget(*)(), doodad(*)());
Copy after login

This function takes two arguments: one is a pointer to a function taking no arguments and returning a gadget, and the other is a pointer to a function taking no arguments and returning a doodad. The function itself returns a widget.

Even more confusing cases arise when extra parentheses are added to function arguments, as in:

widget w(gadget(x));
Copy after login

This looks like it should declare a variable named x of type gadget, but it actually declares a function that takes a first argument named x of type gadget and returns a widget.

The above is the detailed content of Is `widget w(gadget(), doodad());` a Variable Declaration in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template