Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Class Vs. Struct

Author
Message
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 22nd Jun 2009 23:49
Hi! I'm not too new to Dark GDK or C++ programming but, I've been wondering what exactly is the difference between a class and a struct and the advantages between them (other then syntax)?

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 23rd Jun 2009 00:01
The only difference between a class and a struct is that the members of a class are by default private, whereas the members of a struct are public by default. (public means that any code can access those members, private means that only code inside that class or struct can access them)

heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 23rd Jun 2009 00:03
Ok, thanks!

AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 23rd Jun 2009 00:09
Also structs inherit publicly by default and classes inherit privately by default.

i like orange
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 23rd Jun 2009 00:21
There has to be more than just that? cause you can just do private: or public: in the class to switch around

AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 23rd Jun 2009 01:04
Quote: "There has to be more than just that?"


Nope.

Quote: "cause you can just do private: or public: in the class to switch around"


Ya, but we're talking about the default behavior. When you don't specify access level in a class it defaults to private. If you do this:



it's the same as:



And for structs it's the same except it's public, not private.

i like orange
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 23rd Jun 2009 02:20
Structures were part of the C language. They allowed data to be stored but they didn't allow for functions or even constructors. They were useful for data collection. I used them primarily for collecting data to dump into a file by pointing to the structure and telling the write function to write out the sizeof(struct whatever) to the file. This made it possible to read in the same data without having to read in individual data types. I don't believe you can do that well with a class. At the very least it would be a waste of storage space.

With the advent of C++ the encapsulation of the functions within structures began. Classes have default constructors. I'm not sure that structures have that built in but it's possible I guess.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 23rd Jun 2009 05:04 Edited at: 23rd Jun 2009 05:06
Quote: "Classes have default constructors. I'm not sure that structures have that built in but it's possible I guess."


Well both a struct and class can have a default constructor if you declare one. I think you're talking about the compiler implicitly declaring one if one isn't already declared? In which case I'm pretty sure the answer is yes, it does. Structs are literally the exact same as classes apart from the different default access levels.

i like orange
sydbod
16
Years of Service
User Offline
Joined: 14th Jun 2008
Location: Just look at the picture
Posted: 23rd Jun 2009 07:55
Quote: "Structs are literally the exact same as classes apart from the different default access levels."


I just spent the better part of an afternoon chasing a bug because of sloppy code creation and a significant difference with pointers to structures and classes. They appear to be handles differently (at least in VC++2008) one to the other when compiled. An observation that was noted.

If we create SOME.CPP:


****NOTE: I did not instantiate a structure for that pointer*****

I can compile the code fine, and from within that SOME.CPP file there is no problems getting to the individual structure members.
ie: "pUserStructure->......" works correctly with no problems (this surprised me).

One can create a SOMEsecond.CPP:


The code still compiles fine but whenever one tries to access that structure from within this second file with "pUserStructure->......", the code still compile fine, but the EXE file will throw an exception and crash when it gets to that code.
Without instantiating "pUserStructure = new SomeStructure;", the code fails(this did not surprise me).

If one does the exact same procedure, but using a class instead of a structure, even without instantiating, the above example will compile and execute with no problems at all (I nearly fell off my chair).

There is more difference between classes and structures and how they are handles than just difference in default access levels. What it is I do not know, but under very strange conditions they are handled differently.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 23rd Jun 2009 08:19
Maybe they are both virtually the same when you use the language correctly? How do you hope to declare a pointer to another class, much less an extern when you've declared the class in a .cpp file? Unless you're including the .cpp . You really should be declaring the class in the header for some.cpp and the pointer to an instance of it from some.cpp and getting strange results from an uninitialized pointer shouldn't be any surprise, isn't the location it points to undefined? Thus whether it works or not should be irrelevant as it's bad coding; there should be no instance(pun!) where you try to dereference an uninitialized pointer.

sydbod
16
Years of Service
User Offline
Joined: 14th Jun 2008
Location: Just look at the picture
Posted: 23rd Jun 2009 09:00
Quote: "when you've declared the class in a .cpp file?"


You are correct,the class and the structure were actually declared in an attached header file to both CPP files, I was just trying to make the sample simple to represent.
I totally agree, both should have been instantiated. All I was trying to say is that under this incorrect code example, they were being treated differently by the compiler.(only one exception is enough to disprove a rule)
Because we have one example where the 2 are being treated differently (even if using faulty code), one can therefor not safely assume that the 2 are treated the same if they are instantiated properly.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 23rd Jun 2009 09:30
Quote: "one can therefor not safely assume that the 2 are treated the same if they are instantiated properly."


You haven't demonstrated this though, all you've demonstrated is that when uninitialized they may point to different regions of memory using MSVC++(I assume) and for some reason this works with classes.

I just tried pretty much what you did and I get an access violation when trying to assign a value to a data member of an uninitialized class instance so it doesn't work for me.

sydbod
16
Years of Service
User Offline
Joined: 14th Jun 2008
Location: Just look at the picture
Posted: 23rd Jun 2009 09:45
Ahhhh, so it looks like I may have jumped the gun with my assumption.
Thanks for pointing that out.

Login to post a reply

Server time is: 2024-10-01 05:44:57
Your offset time is: 2024-10-01 05:44:57