Topic: struct vs. class


Author: legalize+jeeves@xmission.com (Phil McRevis)
Date: Mon, 30 Oct 2000 18:25:36 GMT
Raw View
[Please do not mail me a copy of your followup]

"Roger Onslow" <roger_onslow@dingoblue.net.au> spake the secret code
<39ee9470$0$11615$7f31c96c@news01.syd.optusnet.com.au> thusly:

>You cannot derive a class from a
>struct, or add member fucntions to a struct etc.
>
>Sounds like a good idea .. its a shame C++ didn't do in the first place.

I disagree.  I find that being able to derive a class from a struct is
very useful, especially for class wrappers around C structs where the
struct is logically treated as an object with "constructor" and
"destructor" functions that must be called for the struct.
--
<http://www.xmission.com/~legalize/> Legalize Adulthood!
    ``Ain't it funny that they all fire the pistol,
      at the wrong end of the race?''--PDBT
    <http://www.xmission.com/~legalize/who/>

---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Tony" <tony@my.isp.net>
Date: Mon, 30 Oct 2000 19:40:47 GMT
Raw View
"Gary Hinger" <garyh@zipperint.com> wrote in message
news:39f0952d_1@news.nwlink.com...
> Which of the following three code fragments do you prefer? They are all
> identical in effect. #2 has the most lines but #1 is my favorite since I
> like to present the interface to my struct/class, not its inner workings.
>
> #1:
> struct Rectangle
> {
>     Rectangle();
>     / *  etc...  * /
>
> private:
>     float m_left, m_top, m_width, m_height; float ugly; std::string crap;
> };
>
> #2:
> class Rectangle
> {
> public:
>     Rectangle();
>     / *  etc...  * /
>
> private:
>     float m_left, m_top, m_width, m_height; float ugly; std::string crap;
> };
>
> #3:
> class Rectangle
> {
>     float m_left, m_top, m_width, m_height; float ugly; std::string crap;
>
> public:
>     Rectangle();
>     / *  etc...  * /
> };

typedef struct
{
     float m_left;
     float m_top;
     float m_width;
     float m_height;
} RectangleType;

const RECTANGLE_TYPE_SZ = sizeof(RectangleType);

class Rectangle: public RectangleType     // private RectangleType?
{
     float ugly;
     float std::string crap;

    public:

    float Left(){return m_left;}
    float Top(){return m_top;}
    float Width(){return m_width;}
    float Height(){return m_height;}
    float Area(){return (m_width*m_height);}
};


---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Gary Hinger" <garyh@zipperint.com>
Date: 2000/10/22
Raw View
For me, most of the annoyance arises from having two keywords (struct and
class) that are almost completely identical. If struct was obliterated from
C++ and I was forced to use class and public:, I wouldn't be complaining.
However, since there are two nearly identical choices, I naturally choose
the best of them and not just the "traditional" one.

My point was that it is both simpler and clearer to just use "struct" and
not "class ... public:" since the two are completely identical as far as the
Standard and every C++ compiler I have ever seen are concerned. Struct
defaults to public access - perfect for defining your interface immediately.
Many people say that it's traditional to use class whenever there are
private members, etc.. but I have yet to hear a good excuse for such bad
habits.

Note that one cannot hardly avoid putting implementation in a class whenever
the class has private data. If someone provides me with a Rectangle class I
probably don't care if it uses members m_left,m_top,m_width,m_height; or
m_left,m_top,m_right,m_bottom, or if they have a member which is an instance
of the RectangleRepresentation class. In any case, I will get to see the
chosen implemenation in the class declaration.

Which of the following three code fragments do you prefer? They are all
identical in effect. #2 has the most lines but #1 is my favorite since I
like to present the interface to my struct/class, not its inner workings.

#1:
struct Rectangle
{
    Rectangle();
    / *  etc...  * /

private:
    float m_left, m_top, m_width, m_height; float ugly; std::string crap;
};

#2:
class Rectangle
{
public:
    Rectangle();
    / *  etc...  * /

private:
    float m_left, m_top, m_width, m_height; float ugly; std::string crap;
};

#3:
class Rectangle
{
    float m_left, m_top, m_width, m_height; float ugly; std::string crap;

public:
    Rectangle();
    / *  etc...  * /
};

John Kewley <kewley@cscs.ch> wrote in message
news:39EFFC51.23D9C25D@cscs.ch...
> In article <39e3b425_1@news.nwlink.com>, Gary Hinger
> <garyh@zipperint.com> writes
> >I
> >personally like struct better because it defaults to public, meaning I
can
> >list the interface to my type immediately. No need for readers to wade
> >through the members in my specific implementation when they just want to
see
> >the interface!
>
> Readers should not need to wade through 'the members ... implementation
> .... to see the interface' in any case for the folowing 2 reasons:
>
> 1. All classes (or structs) should have the public stuff first so the
>    interface to the class is clearer for its readers/users. Adding
'public:'
>    as the first line is a common way to do this.
>
> 2. You should avoid 'implementation' in the class interface altogether.
>
> Cheers,
>
> --
> John M. Kewley                             Tel: +41 (0) 91 610 8248
> Senior Scientist                           Fax: +41 (0) 91 610 8282
> Swiss Center for Scientific Computing      mailto:John.Kewley@cscs.ch
> Via Cantonale, CH-6928 Manno, Switzerland  http://www.cscs.ch/%7Ekewley/
>
> ---
> [ 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.research.att.com/~austern/csc/faq.html                ]
> [ Note that the FAQ URL has changed!  Please update your bookmarks.     ]
>



======================================= MODERATOR'S COMMENT:
 Please do try to keep any follow ups on-topic, i.e., relevant to the C++ Standard and not just to preferred usage of struct/class.


---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 2000/10/23
Raw View
In article <39f0952d_1@news.nwlink.com>, Gary Hinger
<garyh@zipperint.com> writes
>Which of the following three code fragments do you prefer? They are all
>identical in effect. #2 has the most lines but #1 is my favorite since I
>like to present the interface to my struct/class, not its inner workings.

If you want your code to be readily understood by others it is sensible
to learn and abide by the idioms of your language of choice. However I
think this is the wrong forum for this discussion.


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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Stephen Howe" <SPAMGUARDstephen.howe@dial.pipex.co.uk>
Date: 2000/10/24
Raw View
Roger Onslow <roger_onslow@dingoblue.net.au> wrote in message
news:39ee9470$0$11615$7f31c96c@news01.syd.optusnet.com.au...
> Interestingly microsoft's C# makes struct and class different .. structs
are
> just POD and classes are ..well.. classes.  You cannot derive a class from
a
> struct, or add member fucntions to a struct etc.

I concur with this. I wish that struct had been reserved for POD like C and
class for real classes. As it is, we have two keywords for essentially the
same thing with a minor difference between. I hate unnecessary keywords.
Still too late crying over spilt milk.

Stephen Howe


---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: James Kuyper <kuyper@wizard.net>
Date: Sun, 15 Oct 2000 03:35:15 GMT
Raw View
Gary Hinger wrote:
>
> > I jus went thru this whole mess with my editor at O'Reilly... he objected
> to
> > my occassional use of "struct", thinking that it somehow confused matters.
> >
> Using class shouldn't be any better or worse than using struct since these
> keywords are nearly identical. Maybe it would be clearer to always stick
> with one keyword instead of alternating between two nearly identical ones.

As long as the language is trapped (by backward compatibility
requirements) into supporting two nearly identical keywords, using them
to hint at a real difference is not necessarily a bad idea. It is a
convention along the same lines as using upper case for MACROS and lower
case for objects, and mixed case for types. Another similar convention
is to use "typename" for template arguments that could include standard
types like "int", and "class" for template arguments that could only be
user defined types.

---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 2000/10/16
Raw View
In article <86bswnqryv.fsf@gabi-soft.de>,
  kanze@gabi-soft.de wrote:
> Francis Glassborow <francis.glassborow@ntlworld.com> writes:
>
> |>  Well that is not the way struct is used idiomatically. Most of us
> |>  use struct for cases where public data is entirely appropriate and
> |>  use class for the rest, we just specify public: on the next line.
>
> That's my use, and you're the third person who has suggested that it
> is
> an almost universal use.  But the standard uses struct whenever all of
> the members are public, including a lot of things that aren't very
> public data oriented (functional objects, for example).
My understanding is that the standard is not intended to be a usage
guide, but rather a summary of the rules. Kind of like learning a
natural (i.e. spoken) language - you can learn all the rules you want,
but to be a fluent speaker you have to learn *how* the rules are applied
(and, when appropriate, broken).

--
Jim
This message was posted using plain text only.  Any hyperlinks you may
see were added by other parties without my permission.
I do not endorse any products or services that may be hyperlinked to
this message.


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Roger Onslow" <roger_onslow@dingoblue.net.au>
Date: 2000/10/19
Raw View
Interestingly microsoft's C# makes struct and class different .. structs are
just POD and classes are ..well.. classes.  You cannot derive a class from a
struct, or add member fucntions to a struct etc.

Sounds like a good idea .. its a shame C++ didn't do in the first place.

--
Roger Onslow
Software Developer
See my articles at http://www.codeguru.com
See the product I am working on at http://www.swishzone.com

"Operator .. give me the number for 911" .. Homer Simpson

"Richard Peters" <R.A.Peters@Student.tue.nl> wrote in message
news:8s28k5$spj$1@news.tue.nl...
> [snip]
> I think a good thing would be to change struct back the way it was in C,
and
> leave class for the OOP stuff.



---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: John Kewley <kewley@cscs.ch>
Date: 2000/10/20
Raw View
In article <39e3b425_1@news.nwlink.com>, Gary Hinger
<garyh@zipperint.com> writes
>I
>personally like struct better because it defaults to public, meaning I can
>list the interface to my type immediately. No need for readers to wade
>through the members in my specific implementation when they just want to see
>the interface!

Readers should not need to wade through 'the members ... implementation
.... to see the interface' in any case for the folowing 2 reasons:

1. All classes (or structs) should have the public stuff first so the
   interface to the class is clearer for its readers/users. Adding 'public:'
   as the first line is a common way to do this.

2. You should avoid 'implementation' in the class interface altogether.

Cheers,

--
John M. Kewley                             Tel: +41 (0) 91 610 8248
Senior Scientist                           Fax: +41 (0) 91 610 8282
Swiss Center for Scientific Computing      mailto:John.Kewley@cscs.ch
Via Cantonale, CH-6928 Manno, Switzerland  http://www.cscs.ch/%7Ekewley/

---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Scott Robert Ladd" <scottrobertladd@hotmail.com>
Date: 2000/10/13
Raw View
I jus went thru this whole mess with my editor at O'Reilly... he objected to
my occassional use of "struct", thinking that it somehow confused matters.

Quite to the contrary, judicious use of "struct" and "class" can make code
clearer, by delineating between simple aggregates (struct) and encapsulated
objects (class).

I use struct for utility objects -- e.g., list elements, tree nodes -- where
the interface is simple or non-existent. Where appropriate, my structs have
constructors and destructors, but if I have mjore than a few member
functions, the struct is better-defined as a class.

If for no other reason, "struct" is necessary for compatability with C code.

** Scott Robert Ladd (http://www.coyotegulch.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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: qrczak@knm.org.pl (Marcin 'Qrczak' Kowalczyk)
Date: 2000/10/13
Raw View
Wed, 11 Oct 2000 16:25:32 GMT, Gary Hinger <garyh@zipperint.com> pisze:

> 2. A template parameter may be declared class, but not struct. For exam=
ple,
> template <struct T> T square( T const &t ) {return t * t;} // Bad
> is forbidden.
>=20
> I think #2 should be dumped from the standard anyway. I always use type=
name
> which makes a lot more sense since the template parameter could very we=
ll
> turn out to be a struct or a float instead of a class!

typename is OK for simple types, but in type templates currently
class is the only legal keyword:

template<template<typename> class T> class C {...};

--=20
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZAST=CAPCZA
QRCZAK

---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Gary Hinger" <garyh@zipperint.com>
Date: 2000/10/13
Raw View
> I jus went thru this whole mess with my editor at O'Reilly... he objected
to
> my occassional use of "struct", thinking that it somehow confused matters.
>
Using class shouldn't be any better or worse than using struct since these
keywords are nearly identical. Maybe it would be clearer to always stick
with one keyword instead of alternating between two nearly identical ones.

> Quite to the contrary, judicious use of "struct" and "class" can make code
> clearer, by delineating between simple aggregates (struct) and
encapsulated
> objects (class).
Why not use class for simple aggregates and struct for encapsulated objects?
Isn't this just a matter of personal taste?

> If for no other reason, "struct" is necessary for compatability with C
code.
Agreed, as long as you don't add virtual functions, etc. to your structs.



---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "C. M. Heard" <heard@vvnet.com>
Date: 2000/10/14
Raw View
"Gary Hinger" wrote:
> ["Scott Robert Ladd" wrote:]
> > Quite to the contrary, judicious use of "struct" and "class" can make code
> > clearer, by delineating between simple aggregates (struct) and encapsulated
> > objects (class).
> Why not use class for simple aggregates and struct for encapsulated objects?
> Isn't this just a matter of personal taste?

Not at all.  Using struct to indicate simple aggregates gives the keyword
the same meaning that it has in C.  To do otherwise, as you suggest, would
perversely contradict long-established usage.

//cmh

---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: kanze@gabi-soft.de
Date: Sun, 15 Oct 2000 00:28:22 GMT
Raw View
"Gary Hinger" <garyh@zipperint.com> writes:

|>  As far as I know, there are only two differences between struct and
|>  class.  Otherwise, struct and class are completely identical. Here
|>  are the differences:

|>  1. struct defaults to public access, whereas class defaults to privat=
e
|>  access
|>  2. A template parameter may be declared class, but not struct. For ex=
ample,
|>  template <struct T> T square( T const &t ) {return t * t;} // Bad
|>  is forbidden.

|>  I think #2 should be dumped from the standard anyway. I always use ty=
pename
|>  which makes a lot more sense since the template parameter could very =
well
|>  turn out to be a struct or a float instead of a class!

#2 is a case of keyword overloading, much like occurs with static.

|>  The existence of two keywords that are so similar is very
|>  annoying. I personally like struct better because it defaults to
|>  public, meaning I can list the interface to my type immediately. No
|>  need for readers to wade through the members in my specific
|>  implementation when they just want to see the interface!

|>  Wouldn't C++ be cleaner if we dumped one or the other of these superf=
luous
|>  keywords? I'd like to dump class and go back to using struct. Does an=
yone
|>  else have an opinions on this issue?

C++ has a clear relationship to C.  C++ classes can, but don't have to
be much than C struct's.  Given the relationship, I generally use the
keyword struct for something that, once constructed, can be used like a
C struct -- all data members public.  And class for everything else.  I
know that it doesn't make any difference to the compiler whether I write
"class S { public :" or "struct S", but I am still happy that there are
two keywords, for what are conceptually two different things.

--=20
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: kanze@gabi-soft.de
Date: Sun, 15 Oct 2000 00:29:18 GMT
Raw View
Francis Glassborow <francis.glassborow@ntlworld.com> writes:

|>  Well that is not the way struct is used idiomatically. Most of us
|>  use struct for cases where public data is entirely appropriate and
|>  use class for the rest, we just specify public: on the next line.

That's my use, and you're the third person who has suggested that it is
an almost universal use.  But the standard uses struct whenever all of
the members are public, including a lot of things that aren't very
public data oriented (functional objects, for example).

--=20
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: comeau@panix.com (Greg Comeau)
Date: 2000/10/11
Raw View
In article <39e3b425_1@news.nwlink.com>,
Gary Hinger <garyh@zipperint.com> wrote:
>Wouldn't C++ be cleaner if we dumped one or the other of these superfluous
>keywords?

Perhaps, but cleaner isn't the only thing at handhere.

>I'd like to dump class and go back to using struct. Does anyone
>else have an opinions on this issue?

As you noted, accto the language, you're close to having
that liberty, but note too, some folks like to use struct
(based upon their over preferences and conventions that is)
for data-only kinda things, or for getting some sense of
struct'ness for C compatibility reasons (perhaps they're passing
it to a C routines, and this gives them some minor support
in that endeavor), whereas class is more for, well, full-blown
classes, type'y kinda things, with their own operatings, locality, etc.

- Greg
--
Comeau Computing / Comeau C/C++ "so close" 4.2.44 betas NOW AVAILABLE
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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Richard Peters" <R.A.Peters@Student.tue.nl>
Date: 2000/10/11
Raw View
"Gary Hinger" <garyh@zipperint.com> wrote in message
news:39e3b425_1@news.nwlink.com...
> As far as I know, there are only two differences between struct and class.
> Otherwise, struct and class are completely identical. Here are the
> differences:
>
> 1. struct defaults to public access, whereas class defaults to private
> access

> The existence of two keywords that are so similar is very annoying. I
> personally like struct better because it defaults to public, meaning I can
> list the interface to my type immediately. No need for readers to wade
> through the members in my specific implementation when they just want to
see
> the interface!
>
The existence of two keywords that are so similar is annoying indeed. I
think however that there's a psychological difference between those two.
Struct is merely for data-only types, like the ones in C, and class is for
OOP, using member functions, inheritance, etc.
The defaulting to public of struct, I find, is no reason to just use struct
instead of class. You can still do
    class foo {
    public:
       member/data/etc
    private:
    }
one line extra, but I like it that it says explicitly that that part is
public.
I think a good thing would be to change struct back the way it was in C, and
leave class for the OOP stuff.

Richard Peters


---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 2000/10/11
Raw View
In article <39e3b425_1@news.nwlink.com>, Gary Hinger
<garyh@zipperint.com> writes
>I
>personally like struct better because it defaults to public, meaning I can
>list the interface to my type immediately. No need for readers to wade
>through the members in my specific implementation when they just want to see
>the interface!

Well that is not the way struct is used idiomatically. Most of us use
struct for cases where public data is entirely appropriate and use class
for the rest, we just specify public: on the next line.


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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/10/11
Raw View
Gary Hinger wrote:
>
> As far as I know, there are only two differences between struct and class.
> Otherwise, struct and class are completely identical. Here are the
> differences:
>
> 1. struct defaults to public access, whereas class defaults to private
> access
> 2. A template parameter may be declared class, but not struct. For example,
> template <struct T> T square( T const &t ) {return t * t;} // Bad
> is forbidden.

You do not "declare" the parameter as "class". Like "static", "class" is
a keyword with more than one distinct meaning. In the context of a
template formal parameter list, it means "this parameter is a type". The
word "class" was originally used (in 1990) to avoid introducing a new
keyword into C++.

Many years later, the keyword "typename" was introduced to specify
that a dependent name in a template is the name of a type. The
keyword "class" could not be used in this way, because it would
lead to ambiguities.

Once "typename" was introduced, it was obviously a superior way
to indicate that a template formal parameter is the name of a
type. (The type need not be a class type.)  C++ syntax was extended
to allow typename as well as class for that purpose.

Disallowing "class" would have invalidated countless C++ texts and
uncountable existing programs around the world.

>
> Wouldn't C++ be cleaner if we dumped one or the other of these superfluous
> keywords? I'd like to dump class and go back to using struct. Does anyone
> else have an opinions on this issue?

C++ would be a lot cleaner if it didn't have to carry history around
with it.  C++ was not privately designed and then released as a finished
product. It evolved in a public arena over a period of 15 years.

--
Steve Clamage, stephen.clamage@sun.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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Gary Hinger" <garyh@zipperint.com>
Date: Thu, 12 Oct 2000 04:09:41 GMT
Raw View
>> A template parameter may be declared class, but not struct
>You do not "declare" the parameter as "class".
Yes, it is probably clearer to say said that one declares the parameter with
class (rather than some other keyword).

>Disallowing "class" would have invalidated countless C++ texts and
>uncountable existing programs around the world.
>
>C++ would be a lot cleaner if it didn't have to carry history around
>with it.  C++ was not privately designed and then released as a finished
>product. It evolved in a public arena over a period of 15 years.

Obviously there have been many other features of C and C++ that once were in
common use yet have been abandoned. C code written 20 years ago will most
likely not compile today. I am merely suggesting that C++ continue to
evolve. In my opinion, it is a small price to pay to search through one's
code and replace one keyword with another, or make some other such simple
change, in order to use a simpler and more rational language that will make
it easier to develop code and standards in the future.



---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Gary Hinger" <garyh@zipperint.com>
Date: 2000/10/11
Raw View
As far as I know, there are only two differences between struct and class.
Otherwise, struct and class are completely identical. Here are the
differences:

1. struct defaults to public access, whereas class defaults to private
access
2. A template parameter may be declared class, but not struct. For example,
template <struct T> T square( T const &t ) {return t * t;} // Bad
is forbidden.

I think #2 should be dumped from the standard anyway. I always use typename
which makes a lot more sense since the template parameter could very well
turn out to be a struct or a float instead of a class!

The existence of two keywords that are so similar is very annoying. I
personally like struct better because it defaults to public, meaning I can
list the interface to my type immediately. No need for readers to wade
through the members in my specific implementation when they just want to see
the interface!

Wouldn't C++ be cleaner if we dumped one or the other of these superfluous
keywords? I'd like to dump class and go back to using struct. Does anyone
else have an opinions on this issue?



---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






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

Gary Hinger wrote:
>

> I think #2 should be dumped from the standard anyway. I always use typename
> which makes a lot more sense since the template parameter could very well
> turn out to be a struct or a float instead of a class!

structs and unions are CLASSES by definition.

But I agree, the fact that the class template parameter may be a non-class
type is confusing, but C++ avoided generating any more reserved words than
it had to, and typename was a realtively late addition to the language.

---
[ 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]