Topic: illegal constructor syntax: (C++ vs. Java)


Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Sat, 6 Jul 2002 06:21:53 GMT
Raw View
In article <b68d2f19.0207041352.2eb0762f@posting.google.com>, Neil
Zanella <nzanella@cs.mun.ca> writes
>Some have stated that it is for compatibility reasons, but it is not
>yet clear to me what these compatibility issues are exactly. Also,
>it is not clear to me whether the issues are for compatibility
>with standard C or pre-ANSI C.

Compatibility with all versions of C, K&R, C89/90, and C99. All of them
permit block scope declarations of functions (though, of course, the
function itself cannot be defined at block scope because C does not
support local functions). This is not the place to go into the rationale
for this feature of C.

The case of a declaration sans parameter list is just a special case
where dis-ambiguation by the normal route is not possible:
int i;
int foo(i);
// create foo as an int copy of i
int bar (int);
// bar is a function ...

But when there is nothing in the parentheses the only alternative would
have been to resurrect the use of void for such disambiguation. However
that would have created a special case. The C++ Standard rule is if a
declaration could be a function declaration it is and alternatives are
provided so that you can write declarations that cannot be function
declarations.

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Pete Becker <petebecker@acm.org>
Date: Sat, 6 Jul 2002 06:21:57 GMT
Raw View
Neil Zanella wrote:
>
> Robert Klemme <bob.news@gmx.net> wrote in message
>
> > you surely wanted to write
> >
> > Foo foo = new Foo();
>
> Yes. In Java everything is a reference and there is no distinction
> between data and pointers to data.

Except when the data is a builtin type, in which case there's no such
thing as a reference.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Robert Klemme <bob.news@gmx.net>
Date: Wed, 3 Jul 2002 16:11:10 GMT
Raw View

Attila Feher schrieb:
>
> Robert Klemme wrote:
> >
> > Neil Zanella schrieb:
> > > no arguments. Then in Java it is possible to say:
> > >
> > > Foo foo();
>
> In C++ this declares a function called foo, taking no arguments and
> returning an object of Foo.  This is due to the syntax of the C language
> and the fact that C++ tries to keep as close as possible to C syntax, so
> it does not break all the C code.  (BTW in C foo would/could take any
> number of arguments.)
>
> > you surely wanted to write
> >
> > Foo foo = new Foo();
> >
> > otherwise i would not know which syntactical construct you are
> > referring to.
>
> I doubt that.  Foo *foo = new Foo(); might make sense. ;-)

not in java - i was referring to the java example.  :-)  the
reference was a bit lost in your quotation.

regards

 robert

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: nzanella@cs.mun.ca (Neil Zanella)
Date: Thu, 4 Jul 2002 22:32:09 GMT
Raw View
"Thorsten Ottosen" <nesotto@cs.auc.dk> wrote in message news:

> BTW, this is (according to GCC 2.95.3-5 anyway) also fine C++ code, that is,
> one can can choose any of
>
> Foo* foo = new Foo();
> Foo* foo = new Foo;

So this syntax works when building objects on the heap with pointers
it seems, but it does not work when building data on the stack.

Foo foo(); // does not work
Foo foo;   // works

However the gcc g++ tends to support a whole bunch of extensions and
sometimes
is also incompatible with the C++ standard in one way or another. What
I would
really like to know is, what does the standard say about this kind of
initializations. Are they legal? Are they illegal?

Thanks,

Neil

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: nzanella@cs.mun.ca (Neil Zanella)
Date: Fri, 5 Jul 2002 03:44:22 CST
Raw View
Barry Margolin <barmar@genuity.net> wrote in message news:<x7kU8.12

> >T foo(void);
>
> No, that declares a function takes exactly 0 arguments.

Yes it does.

> T foo()
>
> specifies that it takes an unspecified number of arguments, for
> compatibility with pre-ANSI C.

Out of curiosity, besides the compatibility reason, why would anyone
want to specify a function with unspecified number of arguments and
how would one go about doing that? Perhaps you meant that it declares
a function that takes an unspecified but FIXED number of parameters.
Is this correct, or can it be defined in such a way that somehow the
number of parameters vary? I don't think so.

Thanks,

Neil

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: nzanella@cs.mun.ca (Neil Zanella)
Date: Fri, 5 Jul 2002 03:44:43 CST
Raw View
Robert Klemme <bob.news@gmx.net> wrote in message

> you surely wanted to write
>
> Foo foo = new Foo();

Yes. In Java everything is a reference and there is no distinction
between data and pointers to data. You are correct in stating that
the above is the correct way of performing such initialization in
Java. In C++ of course, references can only be initialized using
other data or references and cannot be declared as standalone
references. The C++ pointer style initialization corresponding
to the one above is:

Foo *foo = new Foo();

This constructs a new Foo object on the heap. However I was rather
wondering about the construction that takes place on the stack.
For an object initialization on the stack in C++, I was wondering
why it is not possible to write:

Foo foo();

instead of

Foo foo;

Some have stated that it is for compatibility reasons, but it is not
yet clear to me what these compatibility issues are exactly. Also,
it is not clear to me whether the issues are for compatibility
with standard C or pre-ANSI C.

Thanks,

Neil

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Barry Margolin <barmar@genuity.net>
Date: Fri, 5 Jul 2002 16:28:26 GMT
Raw View
In article <b68d2f19.0207041342.7a3ba32@posting.google.com>,
Neil Zanella <nzanella@cs.mun.ca> wrote:
>Barry Margolin <barmar@genuity.net> wrote in message news:<x7kU8.12
>
>> >T foo(void);
>>
>> No, that declares a function takes exactly 0 arguments.
>
>Yes it does.
>
>> T foo()
>>
>> specifies that it takes an unspecified number of arguments, for
>> compatibility with pre-ANSI C.
>
>Out of curiosity, besides the compatibility reason, why would anyone
>want to specify a function with unspecified number of arguments and
>how would one go about doing that?

AFAIK, compatibility is the only reason.  It was included in the C standard
(at least in C90, I'm not sure about C99) so that old declarations would
still be valid (except for varargs functions, which they decided should
require the "..." in the declarations).

--
Barry Margolin, barmar@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Neil Zanella <nzanella@cs.mun.ca>
Date: Mon, 1 Jul 2002 22:12:35 GMT
Raw View
Hello,

I would like to know why the C++ standard decided to make the following
constructor syntax illegal. After all, in languages such as Java it is
perfectly fine. Suppose I have a constructor for class Foo which takes
no arguments. Then in Java it is possible to say:

Foo foo();

but in C++ one must say:

Foo foo;

Why was C++ designed this way?

Regards,

Neil

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jhyslop@ieee.org (Jim Hyslop)
Date: Mon, 1 Jul 2002 23:05:08 GMT
Raw View
In article <Pine.LNX.4.44.0207010225280.6536-100000@garfield.cs.mun.ca>,
nzanella@cs.mun.ca says...
>
> Hello,
>
> I would like to know why the C++ standard decided to make the following
> constructor syntax illegal. After all, in languages such as Java it is
> perfectly fine. Suppose I have a constructor for class Foo which takes
> no arguments. Then in Java it is possible to say:
>
> Foo foo();
>
> but in C++ one must say:
>
> Foo foo;
>
> Why was C++ designed this way?
History and backwards compatibility.

C has no constructors, so in C a statement such as:

  T foo();

can *ONLY* be a function declaration: foo is a function which takes an
unspecified number of arguments and returns a type T.

In C++, though, the ctor adds a bit of complexity. You might think that

  Foo foo();

"obviously" declares an object of type foo, which is default initialized
- but what about

  int i();

Is that a declaration of a function 'i' which takes no parameters and
returns an int, or is it a definition of an integer 'i' which uses the
default ctor? Well, in C it's a function - imagine how confusing it
would be in C++ if it defined a variable instead.

--
Jim

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Robert Klemme <bob.news@gmx.net>
Date: 2 Jul 2002 07:35:08 GMT
Raw View

Neil Zanella schrieb:
> no arguments. Then in Java it is possible to say:
>
> Foo foo();

you surely wanted to write

Foo foo = new Foo();

otherwise i would not know which syntactical construct you are
referring to.

regards

 robert

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Attila Feher <Attila.Feher@lmf.ericsson.se>
Date: Tue, 2 Jul 2002 15:46:39 GMT
Raw View
Robert Klemme wrote:
>
> Neil Zanella schrieb:
> > no arguments. Then in Java it is possible to say:
> >
> > Foo foo();

In C++ this declares a function called foo, taking no arguments and
returning an object of Foo.  This is due to the syntax of the C language
and the fact that C++ tries to keep as close as possible to C syntax, so
it does not break all the C code.  (BTW in C foo would/could take any
number of arguments.)

> you surely wanted to write
>
> Foo foo = new Foo();
>
> otherwise i would not know which syntactical construct you are
> referring to.

I doubt that.  Foo *foo = new Foo(); might make sense. ;-)

Attila

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: nzanella@cs.mun.ca (Neil Zanella)
Date: Tue, 2 Jul 2002 15:46:22 GMT
Raw View
jhyslop@ieee.org (Jim Hyslop) wrote in message:

> C has no constructors, so in C a statement such as:
>
>   T foo();
>
> can *ONLY* be a function declaration: foo is a function which takes an
> unspecified number of arguments and returns a type T.

Actually, this may be true of pre-ANSI C, but last time I checked I think
the C standard mandated that such a function declaration should be of the
form:

T foo(void);

Then again, many compilers still support pre-ANSI C.
In fact, gcc does not even complain when I give it the
-Wall -ansi -pedantic flags although it probably should.

Regards,

Neil

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Thorsten Ottosen" <nesotto@cs.auc.dk>
Date: Tue, 2 Jul 2002 15:50:12 GMT
Raw View
"Robert Klemme" <bob.news@gmx.net> wrote in message
news:3D215330.FC4760FB@gmx.net...
>
>
> Neil Zanella schrieb:
> > no arguments. Then in Java it is possible to say:
> >
> > Foo foo();
>
> you surely wanted to write
>
> Foo foo = new Foo();
>

BTW, this is (according to GCC 2.95.3-5 anyway) also fine C++ code, that is,
one can can choose any of

Foo* foo = new Foo();
Foo* foo = new Foo;

Thorsten
nesotto@cs.auc.dk


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Barry Margolin <barmar@genuity.net>
Date: Tue, 2 Jul 2002 16:14:02 GMT
Raw View
In article <b68d2f19.0207012148.5d426854@posting.google.com>,
Neil Zanella <nzanella@cs.mun.ca> wrote:
>jhyslop@ieee.org (Jim Hyslop) wrote in message:
>
>> C has no constructors, so in C a statement such as:
>>
>>   T foo();
>>
>> can *ONLY* be a function declaration: foo is a function which takes an
>> unspecified number of arguments and returns a type T.
>
>Actually, this may be true of pre-ANSI C, but last time I checked I think
>the C standard mandated that such a function declaration should be of the
>form:
>
>T foo(void);

No, that declares a function takes exactly 0 arguments.

T foo()

specifies that it takes an unspecified number of arguments, for
compatibility with pre-ANSI C.

--
Barry Margolin, barmar@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]