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?
Jun 7 '07
19 2562
On 7 Jun, 12:17, desktop <f...@sss.comwr ote:
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/archi...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
Erik Wikström wrote:
On 7 Juni, 13:17, desktop <f...@sss.comwr ote:
>Anonymous wrote:
>>On Jun 7, 1:55 pm, Zeppe <zep_p@.remov e.all.this.long .comment.yahoo. itwrote: 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. 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. Readhttp://www.parashift.c om/c++-faq-lite/classes-and-objects.html#fa q-7.8 Regards, 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."
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/archi...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.
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 Wikström
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.c omment.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 Wikström
On Thu, 07 Jun 2007 12:19:15 +0100, Zeppe 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.
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.c omment.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 7 Jun, 12:37, desktop <f...@sss.comwr ote:
Erik Wikström wrote:
On 7 Juni, 13:17, desktop <f...@sss.comwr ote:
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/archi...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.
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.
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?
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-lit...s.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.comwr ote:
[...]
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/archi...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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 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.c omment.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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
James Kanze wrote:
>>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?
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
Erik Wikström wrote:
On 7 Juni, 13:17, desktop <f...@sss.comwr ote:
>Anonymous wrote:
>>On Jun 7, 1:55 pm, Zeppe <zep_p@.remov e.all.this.long .comment.yahoo. itwrote: 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. 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. Readhttp://www.parashift.c om/c++-faq-lite/classes-and-objects.html#fa q-7.8 Regards,
>>>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."
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/archi...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.
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brad Tilley |
last post by:
I don't understand classes very well... maybe some day. Is it just me or
are they supposed to be difficult to understand? They make my head hurt.
Anyway, because I don't understand classes well, I avoid using them.
However, many modules in the standard library are implemented as
classes: sgmllib, HTMLParser, etc. Is it possible to use these modules
without getting into OO programming and inheritance and all the other
lofty, theoretical...
|
by: Pratts |
last post by:
I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??
|
by: Bill |
last post by:
I would like to create a static array of classes (or structs) to be used in
populating name/value pairs in various WebForm drop down list boxes, but am
not quite sure of the construct (or rather to use structs instead of classes
in the array insofar as structs vs. classes appears to be controversial in
C# -- with some recommending avoiding structs altogether).
It needs to be an array something like this:
struct NoteValue
{
|
by: Christopher Ireland |
last post by:
Hi --
I'm trying to find an example of a nested class implemented within the .NET
Framework itself but without much success. I don't suppose that anybody
knows of such an example off the top of their head, do they?
Many thanks!
--
Best Regards,
|
by: Kiran A K |
last post by:
Hi,
can anyone give specific examples where structs are preferred to classes?
| |
by: Bilgehan.Balban |
last post by:
Hi,
I am currently brushing up my c++ knowledge and I would like to ask you
about the differences between classes and C structs, in the
function/method perspective.
1) Is it correct to say that, a structure definition that includes
function pointers only defines the function prototypes to be used with
them, but not the actual implementations, whereas in C++, member
functions cannot be changed *unless* virtual functions are used, or the
|
by: Markus Svilans |
last post by:
Hi,
What is the difference between having a struct with constructors and
methods versus a class?
In many C++ examples online I have seen code similar to this:
struct Animal
{
Animal()
|
by: giddy |
last post by:
hi ,
i'm a C / C# programmer .. have'nt done C++,
in C# .. . object instances of classes ( TextBox txt = new TextBox();)
are reference types. Structs on the other hand are value types.
In C++ i knw there are a few difference between classes and structs but
i need to know if there are value or refrence types.
|
by: Dom |
last post by:
I'm really confused by the difference between a Struct and a Class?
Sometimes, I want just a group of fields to go together. A Class
without methods seems wrong, in that it carries too much overhead (I
think). A Struct seems more appropriate. At least it is what I would
have used in other languages. But since a Struct *can* hold methods,
I wander if I am saving anything. If not, why use it?
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |