473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

You learn something new every day...

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
Jul 22 '05 #1
5 1098
Allan Bruce wrote:
Whilst reading Accelarated C++, I found out that structs in C++ are almost
identical to classes.
[...]
Are there any other differences?


No.

--
Regards,
Buster.
Jul 22 '05 #2
Allan Bruce wrote:
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?


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;
}
Jul 22 '05 #3
Jack Applin wrote:

Are there any other differences?


Yes. A struct defaults to public inheritance, whereas a class defaults
to private inheritance.


[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.
Jul 22 '05 #4

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:c5**********@news.freedom2surf.net...

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?


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.
Jul 22 '05 #5
"jeffc" <no****@nowhere.com> wrote in message news:<40********@news1.prserv.net>...
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:c5**********@news.freedom2surf.net...

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?


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.


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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Rob | last post by:
i know javascript, vbscript, asp css and alot more and im only 14 i was wondering which is easier to learn php or cgi. any help?
42
by: Bicho Verde | last post by:
I have now free time and money to do what I want :-) I have some basic skills in programming (C, Pascal, Macromedia Actionscript) but don't know exactly what to do in the world of programming. And...
24
by: Charif Lakchiri | last post by:
Okay, here's what I know so far about Python: It's an object-oriented scripting language, supported on many platforms. Now here are my questions: It is easy to learn? Does it support GUI...
8
by: Aziz McTang | last post by:
Hi Group, I am not an experienced programmer at all. I've learned html and css well enough to hand-write simple websites. I'm now looking to move to the next step. Initially, I'd like to do 3...
55
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
22
by: Ramzy Darwish | last post by:
Hello, I have a Bachelors in CS and a Masters in Comp. Graphics. In all of my schoolwork, I used C and C++ and thought that I had a pretty good understanding of the language(s). But now, as I...
13
by: Viken Karaguesian | last post by:
Hello everyone, Can anyone recommend a good online site to learn PHP? The W3Schools website is quite lacking - leaves much to be desired. I'm sure there are many places, but which ones are good?...
40
by: dydx13 | last post by:
Hello, I'm currently attending a university majoring in Computer Science. I took C and C++ at a community college and got an A in both classes. That was three years ago. I have not programmed or...
97
by: Master Programmer | last post by:
Thinking of learning VB.NET? New programmer? Thinking of Moving over from VB 6.0? Read on friend, let me help you make a more informed decision......... Microsoft are a pathetic company,...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.