struct vs class

Started by
42 comments, last by ChocoArcher 18 years, 10 months ago
so i've learned to program through JAVA and C++ in school. I use classes like I breath air. But, I've yet to ever use a struct. Recently, to my surprise, I ran into a bunch of structs that included functions. I didn't know structs could have methods (behavior)! I thought they could only have data(state). I understand the idea of having a struct include function pointers, so my question is: if structs can have methods, what are the primary differences between structs and classes in C++?
Advertisement
Quote:Original post by datadawgx
primary differences between structs and classes in C++?


absolutely none, structs defaults to public access and public inheritance while classes defaults to private access and private inheritance.
structs are, I believe, a throwback to C and are kept around for backwards compatability.

The only time I use a struct is when I simply need a container of related values but no methods.. though a public class would be exactly the same.
Quote:Original post by pragma Fury
structs are, I believe, a throwback to C and are kept around for backwards compatability.

The only time I use a struct is when I simply need a container of related values but no methods.. though a public class would be exactly the same.


yeah, I do that, but I end up defining action methods, and it ends up being a serial data access class :P
I use structs as interfaces :)
Quote:Original post by Saruman
I use structs as interfaces :)
If you're using a Microsoft platform, consider using the __interface keyword rather than struct/class, as that gives you some help during compilation. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/key_f-r_3.asp

Quote:Original post by snk_kid
Quote:Original post by datadawgx
primary differences between structs and classes in C++?

absolutely none
he says, and goes on to describe the differences

Quote:Original post by Anonymous Poster
Quote:Original post by snk_kid
Quote:Original post by datadawgx
primary differences between structs and classes in C++?

absolutely none
he says, and goes on to describe the differences


It was clear that he was pointing out that they have none of the larger differences the OP had in mind.

Quote:Original post by Anonymous Poster
Quote:Original post by snk_kid
Quote:Original post by datadawgx
primary differences between structs and classes in C++?

absolutely none
he says, and goes on to describe the differences


[headshake] That does not change the fact there is no difference as you can just specify not to do the default and you can do the same stuff in both.

Also not matter how people use structs the only C++ types compatible with C are POD-types, classes can just as easily be POD-types as can structs just as easily be NON POD-types.
Quote:Original post by Anonymous Poster
Quote:Original post by snk_kid
Quote:Original post by datadawgx
primary differences between structs and classes in C++?

absolutely none
he says, and goes on to describe the differences



Quote:Original post by Anonymous Poster
Quote:Original post by Saruman
I use structs as interfaces :)
If you're using a Microsoft platform, consider using the __interface keyword rather than struct/class, as that gives you some help during compilation. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/key_f-r_3.asp


Please note that these posts were made by different APs ;)

Best Regards,
//AP1

This topic is closed to new replies.

Advertisement