473,499 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Noob question: why the confusing syntax (data typevariable_name(value)) for initialization

What is the need of this syntax for initializing values, isn't this
ambiguous to function call?
e.g., int func_name(20); this looks like function call (of course not
totally as there is no function body), but is declaration and
initialization of an integer variable 'func_name', is it step towards
completeness of OOP abstraction in the language, here one could say we
are creating and initializing an integer object, but then again it
(this way of object initialization) is not so much OOPy (so to speak),
so, maybe that's not true.

Thanks for your time

Krishna
Sep 23 '08 #1
7 1511
krishna wrote:
What is the need of this syntax for initializing values, isn't this
ambiguous to function call?
e.g., int func_name(20); this looks like function call (of course not
totally as there is no function body), but is declaration and
initialization of an integer variable 'func_name', is it step towards
completeness of OOP abstraction in the language, here one could say we
are creating and initializing an integer object, but then again it
(this way of object initialization) is not so much OOPy (so to speak),
so, maybe that's not true.

Thanks for your time

Krishna
Looks like a function call with no function body? When did function calls start
to have a body? You mean function declaration? When did function declarations
start to take a const as an argument? I am not so sure about the reasoning
behind why the language allows such an initialization, though nothing object
oriented or ambiguous about it, it follows the same pattern as member
initialization in the constructor definitions.

class X
{ int y;
public:
X() : y(10) {}
};
Sep 23 '08 #2
krishna wrote:
What is the need of this syntax for initializing values, isn't this
ambiguous to function call?
How is that ambiguous? Type followed by a name is [normally] an object
declaration. A object declaration followed by parenthesized expression
or the symbol '=' followed by an expression is initialisation. And
since it's a declaration statement it cannot be confused with a function
call. A function call can only appear within an expression.
e.g., int func_name(20); this looks like function call
No, it doesn't. It starts with 'int', so it's a declaration.
(of course not
totally as there is no function body),
A function *call* does not require a body.
but is declaration and
initialization of an integer variable 'func_name', is it step towards
completeness of OOP abstraction in the language,
"OOP abstraction"? Not sure what you mean. But the initialisation
syntax is common for all types, fundamental and user-defined alike.
If you define a class that has a constructor that can be called with
a single argument, you can write

myclass object(singleargument);

does that confuse you as well? Does that look like a function call?
Well, it is. It's a statement that *results* in a call to the class
constructor. To generalise the syntax one needs to allow the same
syntax for other scalar types, integral, pointers, references.
here one could say we
are creating and initializing an integer object, but then again it
(this way of object initialization) is not so much OOPy (so to speak),
so, maybe that's not true.
Object called 'func_name' is certainly misleading. OOPy or not, the
statement you brought for this discussion *is* an object declaration
and initialisation. Objects in C++ have the type and they are often
associated with a name. In your case it's an object of type 'int'
and it has the name 'func_name'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 23 '08 #3
"krishna" <kr************@gmail.comwrote in message news:eb**********************************@x16g2000 prn.googlegroups.com...
What is the need of this syntax for initializing values, isn't this
ambiguous to function call?
e.g., int func_name(20); this looks like function call (of course not
totally as there is no function body), but is declaration and
initialization of an integer variable 'func_name', is it step towards
completeness of OOP abstraction in the language, here one could say we
are creating and initializing an integer object, but then again it
(this way of object initialization) is not so much OOPy (so to speak),
so, maybe that's not true.
Note that the alternative way of initializing

int func_name = 20;

looks ambiguous, as well. It looks like an assignment.
In C++ assignment is very different from initialization.
So, both ways of writing an initializer are "ambiguous".
The ambiguity is solved by the context in both cases.
Sep 24 '08 #4
krishna <kr************@gmail.comwrites:
What is the need of this syntax for initializing values, isn't this
ambiguous to function call?
My theory is that it's because C and C++ language designers
are lazy bums who can't type with more than two fingers.

So they had somewhere code to parse ident(expr[,expr]*),
and since they need to match a member ident to an expression,
they just copy-and-pasted the grammar rule and the parsing code.

See:
http://steve-yegge.blogspot.com/2008...le-secret.html
--
__Pascal Bourguignon__
Sep 24 '08 #5
On Sep 24, 1:06 am, krishna <krishna.k.0...@gmail.comwrote:
What is the need of this syntax for initializing values, isn't
this ambiguous to function call?
History. It's generally recognized as an embarassing feature.
(I believe it has been referred to as "C++'s most embarassing
parse".)
e.g., int func_name(20); this looks like function call (of
course not totally as there is no function body), but is
declaration and initialization of an integer variable
'func_name',
You mean it looks like a function declaration, not a function
call. And it gets worse. Consider:

int f( x ) ;

Whether this declares a variable initialized with x, or a
function which takes an argument of type x, depends on whether x
is declared as a variable or a type. Or:

T1 f( T2( x ) ) ;

This is a declaration of a function, always (supposing T1 and T2
are types). Even if what you meant was to explicitly convert x
to type T2, and use it to initialize a variable of type T1.
This leads to some very strange error messages at times:

std::vector< int v( std::istream_iterator< int >( source ),
std::istream_iterator< int >() ) ;

actually declares a function named v, which takes an
std::istream_iterator<intas its first argument, a pointer to a
function which returns an std::istream_iterator<intas its
second argument, and returns an std::vector<int>. Which is
perfectly legal, but when you try to use v later, as a vector,
you get some strange error messages about a function not being
legal in such and such a context.

If the language were being designed from scratch, with no
concerns of backwards compatiblity or history, I'm sure that the
declaration syntax would be significantly different. But it's
not, and wasn't. In the early days, and even now to some
extent, there is a requirement of C compatibility. And since
the declaration syntax in C is horribly broken, no matter what
C++ does with it will cause problems somewhere.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 24 '08 #6
On Sep 24, 10:28 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
krishna <krishna.k.0...@gmail.comwrites:
What is the need of this syntax for initializing values, isn't this
ambiguous to function call?
My theory is that it's because C and C++ language designers
are lazy bums who can't type with more than two fingers.
My theory is that C's designers only had teletypes. If you've
ever worked near a teletype which is outputting something, you
can understand their desire to reduce the number of characters
to a minimum.

As for the motivation of C++'s designer, it's clear: C
compatibility. (I think it was Stroustrup who categorized C's
declaration syntax as an experiment which failed.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 24 '08 #7
krishna wrote:
What is the need of this syntax for initializing values, isn't this
ambiguous to function call?
e.g., int func_name(20); this looks like function call (of course not
totally as there is no function body), but is declaration and
initialization of an integer variable 'func_name', is it step towards
completeness of OOP abstraction in the language, here one could say we
are creating and initializing an integer object, but then again it
(this way of object initialization) is not so much OOPy (so to speak),
so, maybe that's not true.
As others have already pointed out, there is no ambiguity in your
example. There are cases in which something clearly meant to be object
construction is seen by the compiler as a function call, but this is not
one of those cases.

I believe the C++ construction syntax was designed to mimic a function
call, because constructors are functions. You're not really "calling" a
constructor, but one is (in principle) being invoked for you when you
use this syntax.
Sep 24 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2086
by: Matt Hollingsworth | last post by:
Hello, Very new to python, so a noob question. When I've written stuff in JavaScript or MEL in the past, I've always adopted the variable naming convention of using a $ as the first character...
14
8184
by: Olumide | last post by:
In the following C++ class, MyClass{ // Members } From which I create two objects (using the default constructor), (1) MyClass *object1 = new MyClass(); // Stack (2) MyClass object2 =...
8
2129
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
13
1530
by: cgough | last post by:
My true programming language is C++. I am at best a VB6 hacker that is just getting into VB.NET. I have a quick question about when to new and when not to new. Consider the following 2 classes....
42
3385
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
2
1896
by: Dan McCollick | last post by:
Hi All, Noob question that I can not seem to figure out: I am trying to implement a screenscraper to pull data from two seperate websites, here is some test code so far: public static void...
9
3158
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
8
7315
by: iluvatar | last post by:
Hi all. How can I initialize an array data member in the "faster" way? For example, suppose I have a class like class Example{ private: double array; public: Example(const double & val0,...
0
1033
by: APA | last post by:
I have a ASP.NET 20 application that is suffering some pretty poor performance. I'm using the Web Application Stress tool to run a script and I see some confusing performance data. As expected the...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.