Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

Classes vs Structs?


There is a lot of info on this topic on google. But in Bjarne Stroustrup
's book page 225 he writes:

"The declaration of Date in the previous subsection (declared as a
struct) provides a set of functions for manipulating a Date. However, it
does not specify that those functions should be the only ones to depend
directly on Date s representation and the only ones to directly access
objects of class Date . This restriction
can be expressed by using a class instead of a struct"

But I don't see why you need a class for this, on page 234 he even shows
that you can have private and public fields and functions in a struct
also. Is it an error in the book that he writes that you need a class to
make data private?

no, it's not an error. Stroustrup doesn't say that you _need_ to create
a class. He says that you can solve that problem creating a class.
Actually, you can solve it also declaring the members private in a
struct. It's deprecated, anyway, such a use of the struct keyword. Read
http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.8

Regards,

Zeppe

On Jun 7, 1:55 pm, Zeppe

There are certain things you got to decide by yourself while coding.
Now if you want to have you code in C++ but you want to use struct
that will depend upon the limits of struct. If there is something
struct can't handle then you got to use class.

Read this : http://carcino.gen.nz/tech/cpp/struct_vs_class.php

Regards,
ar

"There are things known and things unknown, in between are The Doors."

On 7 Jun, 11:05, Anonymous <ashwani.ra@gmail.com> wrote:

> There are certain things you got to decide by yourself while coding.
> Now if you want to have you code in C++ but you want to use struct
> that will depend upon the limits of struct. If there is something
> struct can't handle then you got to use class.

What limits do you think struct has? What do you think struct can't
handle that class can?

That link describes the near-equivalence of struct and class (although
where it talks of members it should talk of members and base classes -
see FAQ 7.8) and doesn't mention any "limit" or anything "struct can't
handle". It appears to contradict what you say above.

Gavin Deane

I have to admit I've not understood your sentence. If I want to have my
code in c++ (yes, we are in c.l.c++) but I want to use struct that will
depend upon the the limits of struct (well, if I use struct it's because
I need something that depends upon the limits of struct...)... yeah, so
what? And for the last sentence, a struct can handle all the things a
class can, as I said. The only difference is the default visibility of
the members.

That says in a longish way the same that the article that I cited,
agrees with me and contradict what you says. And, on top of that, the OP
didn't have any doubt about the fact that a C++ struct behaves the same
as a c++ class, so I can't really understand the point of the post.

Regards,

Zeppe

On Thu, 07 Jun 2007 09:55:14 +0100, Zeppe wrote:
>no, it's not an error. Stroustrup doesn't say that you _need_ to create
>a class. He says that you can solve that problem creating a class.
>Actually, you can solve it also declaring the members private in a
>struct. It's deprecated, anyway, such a use of the struct keyword.

In what language?

--
Gennaro Prota -- C++ Developer, For Hire
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)

Ok so it seems that there is no difference between structs and classes
in C++ besides the visibility. But in this article:

http://blog.devstone.com/aaron/archive/2004/06/27/205.aspx

the author mentions that structs are passed as value while classes are
passed as reference. So if I know that my struct/class will contain a
lot of data I would go for a class since all the content will not be
copied each time I pass it to a function.

He also describes that you cannot have a constructor without parameters
in a struct, but that seems to work fine, this compiles:

struct bigblop {
        bigblop(){
                a = 345;
        }
        int a;
        int b;

        int geta() {
                return a;
        }

        void seta(int i) {
                a= i;
        }

Gennaro Prota wrote:
> On Thu, 07 Jun 2007 09:55:14 +0100, Zeppe wrote:

>> no, it's not an error. Stroustrup doesn't say that you _need_ to create
>> a class. He says that you can solve that problem creating a class.
>> Actually, you can solve it also declaring the members private in a
>> struct. It's deprecated, anyway, such a use of the struct keyword.

> In what language?

C++, what else? Just saying that defining classes with the struct
keyword is considered deprecated, and that usually the struct keyword is
used mostly for simple data structures. It's not formally deprecated,
but it's a bad habit, such as using macros and stuff like that.

Regards,

Zeppe

On 7 Juni, 13:17, desktop <f@sss.com> wrote:

The author is talking about Managed C++ (a predecessor to C++/CLR), in
which there was some rule like that. In C++/CLR you have to declare
the class *or* struct to be either of value- of ref-type. This however
is totally off-topic in here and (as others have pointed out) the only
difference between struc and class in C++ is the default visibility of
members.

--
Erik Wikstrm

On 7 Jun, 12:17, desktop <f@sss.com> wrote:

> Ok so it seems that there is no difference between structs and classes
> in C++ besides the visibility. But in this article:

> http://blog.devstone.com/aaron/archive/2004/06/27/205.aspx

That article is not about C++. In C++, the technical differences
between struct and class are as described in FAQ 7.8 and no more.

Gavin Deane

Ok so the fact that structs are allocated on the stack and classes are
allocated on the heap does not apply to the current version of C++ either?
On 7 Juni, 13:19, Zeppe <zep_p@.remove.all.this.long.comment.yahoo.it>
wrote:

> Gennaro Prota wrote:
> > On Thu, 07 Jun 2007 09:55:14 +0100, Zeppe wrote:

> >> no, it's not an error. Stroustrup doesn't say that you _need_ to create
> >> a class. He says that you can solve that problem creating a class.
> >> Actually, you can solve it also declaring the members private in a
> >> struct. It's deprecated, anyway, such a use of the struct keyword.

> > In what language?

> C++, what else? Just saying that defining classes with the struct
> keyword is considered deprecated, and that usually the struct keyword is
> used mostly for simple data structures. It's not formally deprecated,
> but it's a bad habit, such as using macros and stuff like that.

I can agree and disagree with this, I can agree that structs are
mostly used with simple structures (PODs) but I don't think that
structs are deprecated. In fact I think it's nice to have both structs
and classes since I then can use structs for PODs and classes for
everything else which makes it easy to separate the two.

Yes, data-hiding and abstraction is great, but sometimes you just need
to keep a a couple of pieces of information together and then a struct
is perfect. The alternative is to use a class with both getters and
setters for all members, which is much worse in my book.

--
Erik Wikstrm

Of course I was being ironical. It's not deprecated in ISO C++. You
don't want to use terms like MS does with their infamous C4996, do
you? ;-)

--
Gennaro Prota -- C++ Developer, For Hire
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)

On 7 Jun, 12:19, Zeppe <zep_p@.remove.all.this.long.comment.yahoo.it>
wrote:

> Gennaro Prota wrote:
> > On Thu, 07 Jun 2007 09:55:14 +0100, Zeppe wrote:

> >> no, it's not an error. Stroustrup doesn't say that you _need_ to create
> >> a class. He says that you can solve that problem creating a class.
> >> Actually, you can solve it also declaring the members private in a
> >> struct. It's deprecated, anyway, such a use of the struct keyword.

> > In what language?

> C++, what else? Just saying that defining classes with the struct
> keyword is considered deprecated, and that usually the struct keyword is
> used mostly for simple data structures. It's not formally deprecated,
> but it's a bad habit, such as using macros and stuff like that.

Since the word "deprecated" has a precise, formal meaning in the
context of standard C++, for the avoidance of doubt and this sort of
miunderstanding it is probably best not to use it to mean something
else unless you make it clear that you are doing so.

Gavin Deane

On Jun 7, 10:55 am, Zeppe

<zep_p@.remove.all.this.long.comment.yahoo.it> wrote:
> desktop wrote:
> > There is a lot of info on this topic on google. But in Bjarne Stroustrup
> > 's book page 225 he writes:
> > "The declaration of Date in the previous subsection (declared as a
> > struct) provides a set of functions for manipulating a Date. However, it
> > does not specify that those functions should be the only ones to depend
> > directly on Date ?s representation and the only ones to directly access
> > objects of class Date . This restriction
> > can be expressed by using a class instead of a struct"
> > But I don't see why you need a class for this, on page 234 he even shows
> > that you can have private and public fields and functions in a struct
> > also. Is it an error in the book that he writes that you need a class to
> > make data private?
> no, it's not an error.

It depends on how you interpret it.  I'm pretty sure that in
this case, Stroustrup is comparing C-style structs to classes;
that by using a class, with everything that C++ supports in
classes, you can express this restriction, which you cannot in a
C style struct.  It has nothing to do with the keyword used to
define the type.

> Stroustrup doesn't say that you _need_ to create
> a class. He says that you can solve that problem creating a class.
> Actually, you can solve it also declaring the members private in a
> struct. It's deprecated, anyway, such a use of the struct keyword.

Not at all.  There's not been the slightest suggestion to
deprecate the keyword struct, at least to my knowledge.  The
fact remains that both keywords are legal, and that in C++,
regardless of which keyword you use, you are defining a class.
As a matter of style, only, many people do make a distinction,
but the exact distinction tends to vary from one person to the
next.

--
James Kanze (GABI Software)             email:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

On 7 Jun, 12:37, desktop <f@sss.com> wrote:

Since that article is not about standard C++, you will only confuse
yourself by trying to learn about standard C++ from that article.
http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.8
tells you all you need to know.

If you find something you think might be a difference between struct
and class, whether in that article or anywhere else, reread FAQ 7.8.
If it's not listed as a difference in FAQ 7.8 then it's not a
difference. It's that simple.

Gavin Deane

On Jun 7, 1:17 pm, desktop <f@sss.com> wrote:

    [...]

> Ok so it seems that there is no difference between structs and classes
> in C++ besides the visibility. But in this article:
> http://blog.devstone.com/aaron/archive/2004/06/27/205.aspx
> the author mentions that structs are passed as value while classes are
> passed as reference.

Are you sure he's not talking about some other language?  If
that article is supposed to be about C++ (it doesn't say), then
the author doesn't know what he's talking about.  (But I rather
suspect that the article might be about C#---I seem to vaguely
recall that in C#, there are both structs and classes, and that
structs work more or less as in C, but classes as in Java.)

> So if I know that my struct/class will contain a
> lot of data I would go for a class since all the content will not be
> copied each time I pass it to a function.

It will in C++.

--
James Kanze (GABI Software)             email:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

On Jun 7, 1:19 pm, Zeppe

<zep_p@.remove.all.this.long.comment.yahoo.it> wrote:
> Gennaro Prota wrote:
> > On Thu, 07 Jun 2007 09:55:14 +0100, Zeppe wrote:
> >> no, it's not an error. Stroustrup doesn't say that you _need_ to create
> >> a class. He says that you can solve that problem creating a class.
> >> Actually, you can solve it also declaring the members private in a
> >> struct. It's deprecated, anyway, such a use of the struct keyword.
> > In what language?
> C++, what else? Just saying that defining classes with the struct
> keyword is considered deprecated, and that usually the struct keyword is
> used mostly for simple data structures. It's not formally deprecated,
> but it's a bad habit, such as using macros and stuff like that.

It's a bad habit according to who?  I've seen many different
conventions in this regard---I generally use struct if the data
members are public, and class if they are private (and they are
always all one or all the other), regardless of the presence or
absence of constructors, member functions, etc.  But that's just
one convention.  Two others I've seen are 1) struct means it's
strictly compatible with C, otherwise, use class, and 2) struct
is used whenever all of the members are public, even if there
aren't any data members (in which case, I use class).  From what
I've seen, I think that Stroustrup uses this last convention,
which means that many functional objects for the STL are
declared using struct.  Are you saying that Stroustrup has bad
habits?

--
James Kanze (GABI Software)             email:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Not quite. For me (and not only for me) providing a class with
constructors, destructors, and member functions AND public member
variables it's *usually* a bad habit (it breaks the basic rules of the
OOP, such information hiding and encapsulation). Having said so, of
course there can be some exceptions to this "rule of thumb", like
function objects, that you have cited. The function objects are not
conceptually objects, they do not encapsulate any concept in the domain
of the problem, etc, so the OOP rules are relaxed for them and there is
no problem in using the struct keyword.

I didn't mean to be very rigid in my previous post, I can't figure out
why so many people got disappointed with my opinion. Probably also
because I used the word "deprecated" as in English language, while it
has got a particular meaning (and implications) in this context, and
that's my bad.

Regards,

Zeppe

Actually he's talking about C# - the old managed extensions for C++ had
no such rule. Also, it is called C++/CLI, not C++/CLR ;)

- Sylvester

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc