#133614 - tepples - Sat Jul 07, 2007 6:12 pm
Quirky wrote: |
A class with no code still may generate the default constructor, destructor, copy constructor and assignment operator. |
How, if class is the same thing as struct?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#133622 - StoneCypher - Sat Jul 07, 2007 8:32 pm
Well, tepples, probably because in C++, structs have constructors and default operators *too*. That's what happens when you never make it through chapter 1 of the C++ book: you make it painfully obvious how clueless you are every time you speak.
Another example is that structs and classes aren't, in fact, the same; that's why there's something called class which isn't the same as struct. (Y'see, C and C++ don't just up and duplicate keywords blindly, that being an idiotic thing to do, and all.)
The reason classes exist is because when Bjarne started extending OO tools into the language, structs were the natural place to do so. However, one of the more important facets of the Smalltalk approach to OO, which is where C++ got its approach, is the idea of information hiding, which sometimes gets called information publicity, and which some people think is called encapsulation, though they're dead wrong.
In the Smalltalk approach to OO, the interface of the UDT should be minimal, and there should be no exposure of data members, nor of internal functions; the outside world should only have access to what's considered the 'external interface.' As such, it is critically important that the default publicity of members be private, not public. However, if that change was made to structs, huge swaths of existing legacy C code would break. As such, a second thing, otherwise identical to a struct, called a class, was created, which had appropriate default publicity.
Now, in traditional Tepples style, I'm sure you're going to pretend you knew all that already. However, it's obvious that you didn't, because if you knew that classes had to be created to deal with limitations in the application of OO mechanisms (such as constructors,) you'd know that structs had those mechanisms.
Well, either that, or you knew structs and classes were very similar, and you were so amazingly stupid that based on that C didn't have constructors in structs, you thought C++ wouldn't either, and as such thus for classes either. Which, surprisingly, seems to be the case:
Quote: |
Quote: | A class with no code still may generate the default constructor, destructor, copy constructor and assignment operator. |
How, if class is the same thing as struct? |
I mean, the depth of braindeadedness and inability to grapple with fundamental, basic language precepts as these is just mind blowing.
Stop trying to pretend to be a programmer, and go get a paper hat with a hamburger on the side. You're dragging the people with potential down by reciting your superstitions developed by years of not reading books and not listening to your betters.
_________________
Quidquid Latine dictum sit, altum Sonatur
[Images not permitted - Click here to view it]
#133627 - tepples - Sat Jul 07, 2007 9:01 pm
StoneCypher wrote: |
Well, tepples, probably because in C++, structs have constructors and default operators *too*. |
What in the standard prevents them from being inlined?
Quote: |
Another example is that structs and classes aren't, in fact, the same; that's why there's something called class which isn't the same as struct. |
What difference does C++ make between class and struct other than the access privileges for members preceding the first public:, protected:, or private: label? I did not mention this at first because I failed to anticipate your pedantic interpretation of the words "the same". If something compiles as both a class as a struct, such as if the first public:, protected:, or private: label precedes the first member, then how does the object code differ? Can you exhibit a test case?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#133628 - StoneCypher - Sat Jul 07, 2007 9:18 pm
Quote: |
What in the standard prevents them from being inlined? |
It's not possible to inline a constructor, you numbskull. Besides, inlining has nothing to do with anything; an object needs those methods in case they're called externally. The object doesn't even have to have an instance to inline in the first place. READ A BOOK.
Quote: |
I did not mention this at first because I failed to anticipate your pedantic interpretation of the words "the same". |
Liar. If you knew they had access privilege differences, you'd know why, and if you knew why they had access privilege differences, you wouldn't be surprised that structs had constructors. You're just lying to get out of caught being wrong, like usual.
Of course, I predicted this up front. If you'd read the things you were replying to, you might have realized I said what you said almost verbatim before you said it; you're such a regular liar that it's become predictable. You could easily be scripted.
Quote: |
If something compiles as both a class as a struct, such as if the first public:, protected:, or private: label precedes the first member, then how does the object code differ? |
Wow, Tepples, you've managed to construct a case in which the difference doesn't apply! SURELY THAT MEANS THE DIFFERENCE IS NEVER THERE.
Quote: |
Can you exhibit a test case? |
I'll exhibit a test case once you display comprehension appropriate to having read two chapters of TC++PL. I am not your private tutor, and frankly I can't stand you (which is de rigeur.) I am not going to sit here wasting my time teaching you the things you're too fucking lazy to learn on your own time.
What I am willing to do is to sit here explaining how utterly worthless your advice is, so that nobody else falls afoul of your stupidity.
Stop wasting my time asking me to do your reading for you. Pick up a book and learn like an adult, you clueless buffoon.
_________________
Quidquid Latine dictum sit, altum Sonatur
[Images not permitted - Click here to view it]
#133630 - tepples - Sat Jul 07, 2007 9:29 pm
StoneCypher wrote: |
Quote: | If something compiles as both a class as a struct, such as if the first public:, protected:, or private: label precedes the first member, then how does the object code differ? |
Wow, Tepples, you've managed to construct a case in which the difference doesn't apply! SURELY THAT MEANS THE DIFFERENCE IS NEVER THERE. |
I have stated the situation that I have seen in all production code. For the record, here is my own test case, which I just compiled:
Code: |
// svc.cpp
// demonstrate the equivalence of struct and class
// in C++
// author: Damian Yerrick
// no rights reserved
// Here I use cstdio because iostream unacceptably
// bloats the executable by introducing a quarter
// megabyte of object code from the C++ standard
// library that is never actually called.
#include <cstdio>
using std::puts;
/* in g++, compile and test like this:
g++ -Wall -O -fno-exceptions -fno-rtti svc.cpp -o svc-struct.exe
g++ -Wall -O -fno-exceptions -fno-rtti -DUSE_CLASS svc.cpp -o svc-class.exe
svc-class.exe
fc /b svc-struct.exe svc-class.exe
*/
// in G++, compile with -DUSE_CLASS or not
#ifdef USE_CLASS
#define CLASS class
#else
#define CLASS struct
#endif
CLASS HelloMessage {
// All C++ books that I have read have stated the best practice
// of always specifying an access privilege before the first
// member, and all production code that I have read follows
// this practice. Under this practice, there is no difference
// between struct and class.
public:
HelloMessage(const char *s):
message(s)
{}
void print() const;
private:
const char *message;
};
void HelloMessage::print() const {
std::puts(message);
}
int main() {
HelloMessage h("Hello World!");
h.print();
return 0;
}
|
You are right that I suck at C++, and I will butt out of C++ language topics in the future. But does this justify your tone?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#133638 - StoneCypher - Sat Jul 07, 2007 10:16 pm
Quote: |
You are right that I suck at C++, and I will butt out of C++ language topics in the future. But does this justify your tone? |
No, but when you continually pepper bad advice after you've been repeatedly notified, when you ask questions that display a startling lack of comprehension, and when you take your attitude, that does.
I'm sorry you're so dense, but there's a reason BRP just told you your tone sucks, and that other people quoted him "for truth." Don't try to pass that off on me. You're the asshole here. You're just getting what you gave.
_________________
Quidquid Latine dictum sit, altum Sonatur
[Images not permitted - Click here to view it]
#133639 - sonny_jim - Sat Jul 07, 2007 10:16 pm
After skimming this, I'm not surprised at all. Whenever I asked for help in #dsdev I got exactly the same attitude from Stonecypher. I'm not going to say anymore for fear of retribution, I just wanted to say that this isn't the first time I've seen him behave like an idiot towards people less knowledgable than him.
#133640 - tepples - Sat Jul 07, 2007 10:24 pm
So what changes should I make to my behavior first? Should I phrase more of my observations as questions? Should I develop a GBA or DS game with no falling blocks? Should I just take a month off?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#133647 - keldon - Sat Jul 07, 2007 10:54 pm
sonny_jim wrote: |
After skimming this, I'm not surprised at all. Whenever I asked for help in #dsdev I got exactly the same attitude from Stonecypher. I'm not going to say anymore for fear of retribution, I just wanted to say that this isn't the first time I've seen him behave like an idiot towards people less knowledgable than him. |
Apart from this line and the next, I am saying nothing! The tone (from Stonecypher) was completely (unnecessary && pointless && childish && unprofessional && antisocial && lame && a_display_of_a_lack_of_social_skills)!
#133665 - StoneCypher - Sat Jul 07, 2007 11:50 pm
Fear of retribution? What are you, retarded?
_________________
Quidquid Latine dictum sit, altum Sonatur
[Images not permitted - Click here to view it]
#133679 - keldon - Sun Jul 08, 2007 12:19 am
Suggestion: a more "social" response would look something like ...
---
In C++, structs have constructors and default operators too. The reason classes exist is because when Bjarne started extending OO tools into the language, structs were the natural place to do so. However, one of the more important facets of the Smalltalk approach to OO, which is where C++ got its approach, is the idea of information hiding, which sometimes gets called information publicity, and which some people think is called encapsulation, though they're dead wrong.
In the Smalltalk approach to OO, the interface of the UDT should be minimal, and there should be no exposure of data members, nor of internal functions; the outside world should only have access to what's considered the 'external interface.' As such, it is critically important that the default publicity of members be private, not public. However, if that change was made to structs, huge swaths of existing legacy C code would break. As such, a second thing, otherwise identical to a struct, called a class, was created, which had appropriate default publicity.
It's not possible to inline a constructor. Besides, inlining has nothing to do with anything; an object needs those methods in case they're called externally. The object doesn't even have to have an instance to inline in the first place.
---
^_^ What do you think? Honestly there is too much disinhibition on the internet. There are further studies about this with columnists implying that many people view the internet as a fantasy world. It just seems that everything is an argument on the internet and people are rapidly disregarding etiquette, respect, and general social skills!
This slows down communication and growth big time. Imagine if 90% of the people in the community had come across 5 antisocial community members and considered the scene to be a little lame! There are probably a lot of people who moved onto other platforms because of this. "Why do GBA Dev, all the people there just spend their whole day flaming people for no apparent reason", I've heard it before and took no notice until I saw this thread. Now I know they are not talking nonsense. This stuff seriously needs to stop; it is childish and pointless. <advice>Maybe take your aggression out on a punch bag, go for a jog, drive the car a few miles an hour slower while playing Frank Sinatra, but for the love of flying bacon don't make the scene look lame.</advice>
#133703 - sajiimori - Sun Jul 08, 2007 1:10 am
Hahah, why do people keep saying it's not possible to inline a constructor?
It's funny how C++ is treated like some kind of mysterious entity when it's been around since the mid '80s. It's a complex language, but seriously, read a book.
#133706 - Optihut - Sun Jul 08, 2007 1:29 am
StoneCypher wrote: |
Stop trying to pretend to be a programmer, and go get a paper hat with a hamburger on the side. You're dragging the people with potential down by reciting your superstitions developed by years of not reading books and not listening to your betters. |
Assuming that you are indeed better, I'd ignore your advice as well, if the presentation is like this. You get what you give.
#133711 - Ant6n - Sun Jul 08, 2007 1:45 am
what does the constructor, destructor etc do if the class is basicly just a struct?
#133717 - tepples - Sun Jul 08, 2007 2:12 am
Citations are the best way I know of to keep from talking out of my behind. So here's an example of my new approach:
By "basically just a struct", I'll assume you're talking about a "POD", or "plain old data",[1] which refers to a primitive type (int, float, pointer, etc.) or a class that 1. has no declared constructor or virtual methods and 2. has only other PODs as members.[2] For each POD type, the compiler infers a trivial constructor,[3] which is default (meaning taking 0 arguments)[4] and does nothing.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#133740 - Lick - Sun Jul 08, 2007 8:04 am
In C++, structs are simply classes with public members by default instead of private ones. So yes, structs can have constructors and destructors.
I believe that in C, structs were POD.
_________________
http://licklick.wordpress.com
#133746 - keldon - Sun Jul 08, 2007 9:47 am
Either way you can avoid the constructor from being called by allocating the memory manually and using pointers right?
#133747 - Ant6n - Sun Jul 08, 2007 9:52 am
i'm no c++ expert, but curios. if very simple c++ objects are local variables, can't they just live on the stack, meaning they dont need memory allocation, but only initialization (which would then be trivial)?
#133762 - wintermute - Sun Jul 08, 2007 2:30 pm
In C/C++ local variables already live on the stack. They still need allocation which is handled by the function epilogue and, in the case of objects with constructors, initialisation is handled automatically.
Stack space on the DS is currently limited to ~16K so having a lot of large local variables isn't the best idea in the world. I've considered doing some trickery to make dtcm overlap main memory so the stack can overflow but it doesn't really seem to be an issue for the vast majority of DS apps.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog
#133821 - sajiimori - Sun Jul 08, 2007 11:47 pm
Keldon,
Yeah, you can cast any address to a pointer to any type, and dereference it at your peril. ;)
Of course, doing this with care can be very handy. Containers like std::vector own buffers large enough to hold multiple objects, then use placement new to construct directly into them.
When you take manual control over the task of calling constructors, destructors, and assignment operators, there are a lot of ways to screw up, but it's sometimes a viable road to efficient behavior.
#133823 - sajiimori - Sun Jul 08, 2007 11:57 pm
To clarify for whoever was asking about compiler-generated constructors and destructors:
If you don't provide a constructor, the compiler provides one for you. It's generally inlined (though inlining is never required by the standard), and it simply calls the constructor of every data member in the order that it was declared.
For compiler-generated constructors that are trivial (in the sense tepples described), this all optimizes away and generates 0 instructions, on any sane compiler.
The same can be said for compiler-generated destructors.
#133906 - ScottLininger - Mon Jul 09, 2007 6:15 pm
Tepples,
Tremendous kudos for keeping your tone and responses on such a respectful level. It's people like you who make this forum such an asset.
-Scott
#134124 - The_Perfection - Tue Jul 10, 2007 8:04 pm
Business first:
I've had similar questions about the differences between classes and structs. The main difference that I see between the two is the default publicity: either public or private. Now, I haven't played with messing with access specifiers in structs, but by tepples's example I would guess that they can be used somewhat interchangeably. (As a side note, I'd like to see a "sizeof()" be placed into that code for both the class and the struct, just to make sure they are the same size (which they probably are).)
Tepples is correct in saying that it is good programming practice to begin class declarations with access specifiers. I have yet to see class code in any book that does not follow this practice. This makies tepples's so-called "constructed case" the "normal case". With this programming practice, the "class" and the "struct" can be used as the same thing.
Essentially, classes and structs are almost exactly the same thing because the access specifiers can essentially be used to change from a class to a struct (and vice versa).
_ _ _
Now to address people...
Thank you, tepples, for staying polite throughout this whole thing.
Okay, okay, I'm gonna try to remain polite whilst saying this, and if I'm not, I apologize.
StoneCypher, while some of the things you say are indeed correct, your tone could use some adjustment. For example, most of your messages consist of something along the lines of "You're stupid," "Go read a book," or a right out insult. If you don't believe me, have a look: StoneCypher wrote: |
...
you make it painfully obvious how clueless you are every time you speak.
...
and you were so amazingly stupid
...
the depth of braindeadedness and inability to grapple with fundamental, basic language precepts as these is just mind blowing.
...
Stop trying to pretend to be a programmer, and go get a paper hat with a hamburger on the side. You're dragging the people with potential down by reciting your superstitions developed by years of not reading books and not listening to your betters.
|
StoneCypher wrote: |
...
It's not possible to inline a constructor, you numbskull.
...
READ A BOOK.
...
Liar.
...
you're such a regular liar that it's become predictable. You could easily be scripted.
...
I'll exhibit a test case once you display comprehension appropriate to having read two chapters of TC++PL.
...
I am not going to sit here wasting my time teaching you the things you're too ****ing lazy to learn on your own time.
...
What I am willing to do is to sit here explaining how utterly worthless your advice is, so that nobody else falls afoul of your stupidity.
...
Pick up a book and learn like an adult, you clueless buffoon. |
StoneCypher wrote: |
...
I'm sorry you're so dense,
...
You're the ******* here.
... |
StoneCypher wrote: |
...
What are you, retarded? |
There are a particular few I'd like to address:
StoneCypher wrote: |
C and C++ don't just up and duplicate keywords blindly, that being an idiotic thing to do |
I've noticed a few incidences of overlapping types, such as (on most systems) int and long, double and long double, and almost class and struct.
StoneCypher wrote: |
it is critically important that the default publicity of members be private, not public. |
I find this important only in a corporate setting, so as to keep people from mistakenly or purposely modifying something they shouldn't. On personal projects I generally write nearly monolithic programs because I know how everything is supposed to go. I also consider writing classes too time consuming if they are not going to be reused.
StoneCypher wrote: |
You're dragging the people with potential down by ... not listening to your betters. |
With the way you act, I would not consider you my better, reguardless of whether or not you are more knowledgeable than me.
StoneCypher wrote: |
You could easily be scripted. |
You can be scripted pretty easily by following the pattern of your posts.
StoneCypher wrote: |
when you continually pepper bad advice after you've been repeatedly notified |
And you contine to "pepper" the forum with a bad tone even after you have been repeatedly notified.
Remember:
You're just getting what you gave.
_ _ _
Okay, I'm done.
In other news, I got really good at typing cypher. ?_?;
#134687 - pepsiman - Sun Jul 15, 2007 9:13 pm
StoneCypher wrote: |
you're such a regular liar that it's become predictable. You could easily be scripted. |
You're the predictable one, you say this about everyone you disagree with.
#134695 - sgeos - Sun Jul 15, 2007 11:12 pm
StoneCypher wrote: |
READ A BOOK. |
Some of the responses in this thread were very disrespectful- it is shocking. Instead of insulting people, offer constructive criticism and suggest something! I just finished this book, and it was quite informative:
C++: The Core Language
By Gregory Satir and Doug Brown.
Published by O'Reilly.
ISBN: 1-56592-116-X
Preface: "This book is for C programmers who want to learn C++."
The comp.lang.c++ faq is also fantastic reading material. If anyone else has any suggestions, by all means bring them up.
-Brendan
#134747 - PeterM - Mon Jul 16, 2007 10:01 am
The C++ FAQ Lite is really good
http://www.parashift.com/c++-faq-lite/
As are any of the Herb Sutter or Scott Meyers books.
Sutter's Guru of the Week is a good read too, covering much of the material in the books.
http://www.gotw.ca/gotw/index.htm
(Start at the bottom of the page and work upwards!)
_________________
http://aaiiee.wordpress.com/
#134952 - Miked0801 - Wed Jul 18, 2007 9:28 pm
Wow. Fun thread. Thank god I didn't ask for any C++ advice here. Being a great C programmer but a C++ noob is fun enough without all the extras of being flamed by strangers on a generally friendly board :)