Topic: Why class X { }; ?


Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/05/09
Raw View
In article <3914A4E0.B5829F00@best.com>, Steve Hardt <hardts@best.com>
writes
>In fact, that's exactly what they do in Java.  You can have a ';' at the
>end, but it's optional.
>
>I'm guessing C++ does it just to be more "C"-like.

Even if you never use it, we need to support defining instances,
pointers to instances and functions returning the defined
class/struct/union/enum as part of the definition statement. We need to
do that for compatibility with C (and for political reasons that was a
major design criterion for C++ syntax.)

Java does not allow you to define an object or a pointer and so the
facility is not necessary. While the designers of Java elected to use
much C syntax (including some very bad cases) this was a place that the
C syntax had no practical use.

It might be a good coding standard to prohibit declarations/defintions
of functions/objects as part of the definition of a UDT.

Providing a warning message when the definition of a UDT does not
terminate with exactly }; might be useful (just as some compilers will
warn about assignments in conditionals)



Francis Glassborow      Association of C & C++ Users
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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Hardt <hardts@best.com>
Date: 2000/05/08
Raw View
In fact, that's exactly what they do in Java.  You can have a ';' at the
end, but it's optional.

I'm guessing C++ does it just to be more "C"-like.

Steve

David R Tribble wrote:
>
> Guojiang Liu wrote:
> >> Could any one explain why do we need ";" at the end of class
> >> declaration?
> >>
> >> //x.h
> >> class X
> >> {
> >> ...
> >> };
>
> chris gaudette wrote:
> > because that defines the end of the class.
>
> More specifically, you can do all of these:
>
>     class X { ... };
>     class   { ... };
>     class X;
>
>     class X { ... } var;
>     class   { ... } var;
>     class X         var;
>
> These last group is equivalent to:
>
>     Type  var;
>
> The 'var' definition is optional, so the '};' simply means you are
> not defining any variables of that particular class type.
>
> In my opinion, C++ syntax should have disallowed definining variables
> and a struct/class in the same contruct. C++ could have made this
> change from C, but alas, it did not.
>
>     struct T { ... } var;    // illegal (I wish)
>
>     struct T { .... }        // better
>     struct T var;
>
> You don't need a ';' following the closing '}' of a function
> definition, so why should you need a ';' following a struct/class/enum
> declaration?  C++ kept too much of C's awkward syntax.
>
> --
> David R. Tribble, mailto:david@tribble.com, http://david.tribble.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://reality.sgi.com/austern_mti/std-c++/faq.html              ]

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David R Tribble <david@tribble.com>
Date: 2000/04/26
Raw View
Guojiang Liu wrote:
>> Could any one explain why do we need ";" at the end of class
>> declaration?
>>
>> //x.h
>> class X
>> {
>> ...
>> };

chris gaudette wrote:
> because that defines the end of the class.

More specifically, you can do all of these:

    class X { ... };
    class   { ... };
    class X;

    class X { ... } var;
    class   { ... } var;
    class X         var;

These last group is equivalent to:

    Type  var;

The 'var' definition is optional, so the '};' simply means you are
not defining any variables of that particular class type.

In my opinion, C++ syntax should have disallowed definining variables
and a struct/class in the same contruct. C++ could have made this
change from C, but alas, it did not.

    struct T { ... } var;    // illegal (I wish)

    struct T { .... }        // better
    struct T var;

You don't need a ';' following the closing '}' of a function
definition, so why should you need a ';' following a struct/class/enum
declaration?  C++ kept too much of C's awkward syntax.

--
David R. Tribble, mailto:david@tribble.com, http://david.tribble.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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: comeau@panix.com (Greg Comeau)
Date: 2000/04/18
Raw View
In article <38FB57EB.287F2F72@objectsciences.net> chris gaudette <chrisg@objectsciences.net> writes:
>Guojiang Liu wrote:
>> Could any one explain why do we need ";" at the end of class
>> declaration?
>>
>> //x.h
>> class X
>> {
>> ...
>> };
>
>because that defines the end of the class.

Yes, but Liu is asking why that is so, IOWs, why does it need to
define the end of the class.  The reason, I believe, is tied into
that once up a time C allowed this:

 /* Implicit int return type */ foo(); // AA

It also allowed a type to be defined when declaring a function:

 struct xyz { int i; } bar(); // BB

But if you really wanted this:

 struct xyz { int i; }; /* implicit int */ bar(); // CC

then if you'd typo'd and left out the semicolon (whether
you wrote it that way, or on two seperate lines, or with
xyz at the end of a header file that you #include'd just before
bar()), then you wouldn't get what you wanted.  And it's not
so much that there was a typo, but in that there needs to
be a way to distinguish between BB and CC.

Actually, all the details are deeper than the above, but that's the
general gist of it.  Anyway, some of this was solved in C:
it requires ; to end a struct (it however, still allowed
implicit int, and for types to be defined when declaring functions).

- Greg
--
Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
Try Comeau C++ online at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: =?ISO-8859-1?Q?J=F6rg?= Barfurth <joerg.barfurth@germany.sun.com>
Date: 2000/04/19
Raw View
> In article <38FB57EB.287F2F72@objectsciences.net> chris gaudette
<chrisg@objectsciences.net> writes:

> >Guojiang Liu wrote:
> >> Could any one explain why do we need ";" at the end of class
> >> declaration?

> >because that defines the end of the class.

Am 18.04.00, 11:50:26, schrieb comeau@panix.com (Greg Comeau) zum Thema
Re: Why class X { }; ?:

> Yes, but Liu is asking why that is so, IOWs, why does it need to
> define the end of the class.  The reason, I believe, is tied into
> that once up a time C allowed this:

>  /* Implicit int return type */ foo(); // AA
> It also allowed a type to be defined when declaring a function:

Even in C++ it is necessary, if you consider local classes:

int x;

void foo() {
    struct X { X(int) {}; }
    x = 2;
    // work with X here
}

Here a ';' would certainly make a difference.

-- J   rg Barfurth


---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: chris gaudette <chrisg@objectsciences.net>
Date: 2000/04/18
Raw View
because that defines the end of the class.
thanks
mr guadeteletes

Guojiang Liu wrote:

> Could any one explain why do we need ";" at the end of class
> declaration?
>
> //x.h
> class X
> {
> ...
> };
>
> Thanks


---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: kanze@gabi-soft.de
Date: 2000/04/11
Raw View
comeau@panix.com (Greg Comeau) writes:

|>  In article <t7og7wb9rj.fsf@calumny.jyacc.com> Hyman Rosen <hymie@prolifics.com> writes:
|>  >You can actually write something like

|>  >struct X { } f() { X x; return x; }

|>  I'm pretty certain that in C you can do this but not in C++.

Can you quote the passage?  (I know that C++ forbids defining new types
in a new expression, and I think as a function parameter, but I thought
the return type was legal.)

In any case, you very definitely can write things like:

    struct X { ... } anX ;

defining the type and declaring a variable in the same statement.

--
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hyman Rosen <hymie@prolifics.com>
Date: 2000/04/11
Raw View
kanze@gabi-soft.de writes:
> Can you quote the passage?  (I know that C++ forbids defining new types
> in a new expression, and I think as a function parameter, but I thought
> the return type was legal.)

It should be, but 8.3.5/6 forbids it.

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: comeau@panix.com (Greg Comeau)
Date: 2000/04/11
Raw View
In article <86og7kcnwu.fsf@gabi-soft.de> kanze@gabi-soft.de writes:
>comeau@panix.com (Greg Comeau) writes:
>>In article <t7og7wb9rj.fsf@calumny.jyacc.com> Hyman Rosen <hymie@prolifics.com> writes:
>>>You can actually write something like
>>>
>>>struct X { } f() { X x; return x; }
>>
>>I'm pretty certain that in C you can do this but not in C++.
>
>Can you quote the passage?  (I know that C++ forbids defining new types
>in a new expression, and I think as a function parameter, but I thought
>the return type was legal.)

Right, and you can't do it for casts, and probably one or two
other places either.  I'm sure not in sizeof() and so I'd imagine
not in typeid too, or with exceptions.  Find the passage?  Grrr...rrrr.
I seem to recall it's spread around.  Let's see.... 8.3.5p6.

>In any case, you very definitely can write things like:
>
>  struct X { ... } anX ;
>
>defining the type and declaring a variable in the same statement.

Agreed, no problem there.

- Greg
--
Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
Try Comeau C++ online at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: comeau@panix.com (Greg Comeau)
Date: 2000/04/02
Raw View
In article <t7og7wb9rj.fsf@calumny.jyacc.com> Hyman Rosen <hymie@prolifics.com> writes:
>You can actually write something like
>
>struct X { } f() { X x; return x; }

I'm pretty certain that in C you can do this but not in C++.

- Greg
--
Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
Try Comeau C++ online at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Robert Klemme <robert.klemme@myview.de>
Date: 2000/03/31
Raw View

Guojiang Liu schrieb:
>
> Could any one explain why do we need ";" at the end of class
> declaration?
>
> //x.h
> class X
> {
> ...
> };
>
> Thanks

you could define an object of this class or even create an object of an
anoymous class:

class X
{
// ...
} myXobject;

class
{
// ...
} anObjectOfAnUnnamedClass;

regards

 robert

--
Robert Klemme
Software Engineer
-------------------------------------------------------------
myview technologies GmbH & Co. KG
Riemekestra   e 160 ~ D-33106 Paderborn ~ Germany
E-Mail: mailto:robert.klemme@myview.de
Telefon: +49/5251/69090-321 ~ Fax: +49/5251/69090-399
-------------------------------------------------------------

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: =?ISO-8859-1?Q?J=F6rg?= Barfurth <joerg.barfurth@germany.sun.com>
Date: 2000/03/31
Raw View

>>>>>>>>>>>>>>>>>> Urspr   ngliche Nachricht <<<<<<<<<<<<<<<<<<

Am 30.03.00, 22:42:48, schrieb "Guojiang Liu" <liug@nortelnetworks.com> zum
Thema Why class X { }; ?:


> Could any one explain why do we need ";" at the end of class
> declaration?

> //x.h
> class X
> {
> ...
> };

int a;

int main()
{
 class X
 {
  X(int) {}
 }

 a = 2; // is this X a = 2; or ::a = 2,
}

hth
J   rg


---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hyman Rosen <hymie@prolifics.com>
Date: 2000/03/31
Raw View
"Guojiang Liu" <liug@nortelnetworks.com> writes:
> Could any one explain why do we need ";" at the end of class
> declaration?
>
> class X { ... };

It's declaration syntax, that's all. It's the same ; that's at the end of

 int i;

You can actually write something like

struct X { } f() { X x; return x; }

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Scott Condit" <scottc@tdv.com>
Date: 2000/04/01
Raw View
Hyman Rosen <hymie@prolifics.com> wrote in message
news:t7og7wb9rj.fsf@calumny.jyacc.com...
> "Guojiang Liu" <liug@nortelnetworks.com> writes:
> > Could any one explain why do we need ";" at the end of class
> > declaration?
> >
> > class X { ... };
>
> It's declaration syntax, that's all. It's the same ; that's at the end of
>
> int i;
>
> You can actually write something like
>
> struct X { } f() { X x; return x; }
>

A type definition in a return type is disallowed; it's in annex C of the
standard.

Scott.



---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hyman Rosen <hymie@prolifics.com>
Date: 2000/04/01
Raw View
"Scott Condit" <scottc@tdv.com> writes:
> A type definition in a return type is disallowed; it's in annex C of the
> standard.

I couldn't find where in the Standard this is actually forbidden,
aside from this mention in the annex. The annex refers to 8.4,
but I couldn't find the restriction there.

Note that it makes no sense to disallow a type definition in a
return type. The annex provides justification only fro parameters.

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/04/01
Raw View

Scott Condit wrote:
>

>
> A type definition in a return type is disallowed; it's in annex C of the
> standard.

Annex C doesn't allow or disallow anything.  It's informative text that details
incompatibility between C and C++.  It does explain this and refer you to section
8.4, but it's really in 8.3.5/6.

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Guojiang Liu" <liug@nortelnetworks.com>
Date: 2000/03/30
Raw View
This is a multi-part message in MIME format.
--------------C41D82B7142613CCEF3722F6
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Could any one explain why do we need ";" at the end of class
declaration?

//x.h
class X
{
...
};

Thanks
--------------C41D82B7142613CCEF3722F6
Content-Type: text/x-vcard; charset=us-ascii;
 name="liug.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Liu, Guojiang [CAR:9Q10:EXCH]
Content-Disposition: attachment;
 filename="liug.vcf"

begin:vcard
n:Liu;Gary (Guojiang)
x-mozilla-html:FALSE
org:Enterprises Data Networks, Nortel Networks;9Q10
adr:;;;;;;
version:2.1
email;internet:liug@nortelnetworks.com
title:Consultant (ATM/IP Switch/Protocol Development
note;quoted-printable:(613) 763-2302 (o)=0D=0A(613) 727-0094 (h)
x-mozilla-cpt:;2368
fn:Gary (Guojiang) Liu
end:vcard

--------------C41D82B7142613CCEF3722F6--

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/03/31
Raw View
In article <38E2AB53.14FC2F6B@americasm01.nt.com>, Guojiang Liu
<liug@nortelnetworks.com> writes
>Could any one explain why do we need ";" at the end of class
>declaration?
>
>//x.h
>class X
>{
>...
>};

Let me be pedantic and correct your question.  A class declaration would
be:

class X;

clearly we need a semi-colon in that case.

What you were asking about is a class definition (OK, definition are
also declarations but we need to focus on the definition aspect).

C++ inherited from C that all UDT (user defined types) definitions can
optionally define one or more variables of the type defined.  For
example x would be a variable of type X, and xp a pointer to an instance
of type X in each of the following:

struct X {/* whatever*/} x, *xp=0;
union X {/* whatever*/} x, *xp=0;
enum X {value} x=value, *xp=0;
class X {/* whatever*/} x, *xp=0;

And a nasty trap is something such as:

class X {/* whatever*/}

fn();

which makes fn a function returning an X and not an implicit int (which
isn't supported by C++ but the omitted semi-colon silences the compiler.


Francis Glassborow      Journal Editor, Association of C & C++ Users
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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/03/31
Raw View
Guojiang Liu wrote:
>
> Could any one explain why do we need ";" at the end of class
> declaration?
>
> //x.h
> class X
> {
> ...
> };

In order to tell where the declaration ends. You're allowed to declare
specific objects based upon the specified type:

class X
{
// ...
} x /* Assumes accessible default ctor.*/, *px=&x, &rx = x;

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]