Connecting Tech Pros Worldwide Forums | Help | Site Map

You learn something new every day...

Allan Bruce
Guest
 
Posts: n/a
#1: Jul 22 '05
Whilst reading Accelarated C++, I found out that structs in C++ are almost
identical to classes. i.e.

struct Foo
{
public:
Get();
private
int mNum;
};

is the same as:

class Foo
{
public:
Get();
private
int mNum;
};

Apparantly the difference between structs and classes is that a class has
default private access, and a struct has default public access! Well, I
never!

Are there any other differences?
Thanks
Allan



Buster
Guest
 
Posts: n/a
#2: Jul 22 '05

re: You learn something new every day...


Allan Bruce wrote:
[color=blue]
> Whilst reading Accelarated C++, I found out that structs in C++ are almost
> identical to classes.[/color]

[...]
[color=blue]
> Are there any other differences?[/color]

No.

--
Regards,
Buster.
Jack Applin
Guest
 
Posts: n/a
#3: Jul 22 '05

re: You learn something new every day...


Allan Bruce wrote:
[color=blue]
> Apparantly the difference between structs and classes is that a class has
> default private access, and a struct has default public access! Well, I
> never!
>
> Are there any other differences?[/color]

Yes. A struct defaults to public inheritance, whereas a class defaults
to private inheritance. This code compiles:

struct foo {
int a;
};

struct bar : foo {
} b;

int main() {
int i = b.a;
}

This code should not compile, because b.a is inaccessible:

struct foo {
int a;
};

class bar : foo {
} b;

int main() {
int i = b.a;
}
Buster
Guest
 
Posts: n/a
#4: Jul 22 '05

re: You learn something new every day...


Jack Applin wrote:[color=blue][color=green]
>>
>>Are there any other differences?[/color]
>
> Yes. A struct defaults to public inheritance, whereas a class defaults
> to private inheritance.[/color]

[I wrote "No."]

You're right, of course. I took the OP's "has default public access"
to mean access to base subobjects as well as access to members.

--
Regards,
Buster.
jeffc
Guest
 
Posts: n/a
#5: Jul 22 '05

re: You learn something new every day...



"Allan Bruce" <allanmb@TAKEAWAYf2s.com> wrote in message
news:c5m3ui$mo5$1@news.freedom2surf.net...[color=blue]
>
> Apparantly the difference between structs and classes is that a class has
> default private access, and a struct has default public access! Well, I
> never!
>
> Are there any other differences?[/color]

Similar idea when inheriting regarding the default, but otherwise they are
more or less 2 words for the same thing. Some programmers will use "struct"
when the "class" has no functions, but this is a style issue, not technical
issue.


Razmig K
Guest
 
Posts: n/a
#6: Jul 22 '05

re: You learn something new every day...


"jeffc" <nobody@nowhere.com> wrote in message news:<407eb31e_4@news1.prserv.net>...[color=blue]
> "Allan Bruce" <allanmb@TAKEAWAYf2s.com> wrote in message
> news:c5m3ui$mo5$1@news.freedom2surf.net...[color=green]
> >
> > Apparantly the difference between structs and classes is that a class has
> > default private access, and a struct has default public access! Well, I
> > never!
> >
> > Are there any other differences?[/color]
>
> Similar idea when inheriting regarding the default, but otherwise they are
> more or less 2 words for the same thing. Some programmers will use "struct"
> when the "class" has no functions, but this is a style issue, not technical
> issue.[/color]

To quote Bjarne Stroustrup:

- Which style you use depends on circumstances and taste. I usually
prefer to use struct for classes that have all data public. I think of
such classes as "not quite proper types, just data structures" .
Constructors and access functions can be quite useful even for such
structures, but as a shorthand rather than guarantors of properties of
the type.

- Use public data (structs) only when it really is just data an no
invariant is meaningful for the data members.

Source: The C++ Programming Language, Third Edition

----
//rk
Closed Thread