Jump to content
  • Advertisement
Sign in to follow this  
KSP

Class vs. Structure?

This topic is 3990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone. My math library is almost ready(except for quaternion code), But I came to a design problem and since I will place some of math code in a separate dll, It is important specially if I had to write some SSE assembly code later. Suppose I had a structure for 4x4 matrix named matrix4x4_t and is defined like this
struct matrix4x4_t
{
  union
  {
    struct
    {
       float _11, _12, _13, _14;
       float _21, _22, _23, _24;
       float _31, _32, _33, _34;
       float _41, _42, _43, _44;
    };
    float data[4][4];
  };
};

But I wanted to rewrite this code and create a Matrix class instead of a structure. Do you think if I do this, the compiler changes member variable alignments or order? I'm asking this because I will add some SSE2 code later in assembly, and in that case alignment and data order is important. Yours, KSP.

Share this post


Link to post
Share on other sites
Advertisement
In C++ a class and a struct are exactly the same except structs default to public access and inheritance, classes default to private access and inheritance and they're spelled differently.

Share this post


Link to post
Share on other sites
Structs and classes are the same in C++, except structs default to public whereas classes default to private. Other than that the compiler shouldn't do anything different, because they aren't.

Cheers

Share this post


Link to post
Share on other sites
Hi.
Thanks for your replies. I'm gonna use classes because I want to add some member functions (I know I can do it with structures, but I don't like it).

Yours,
KSP.

Share this post


Link to post
Share on other sites
Quote:
Original post by KSP
Hi.
Thanks for your replies. I'm gonna use classes because I want to add some member functions (I know I can do it with structures, but I don't like it).

Yours,
KSP.

So instead of typing:
struct matrix
{
union ...
float some_function(...) { }
};

You will type:
class matrix
{
public:
union ...
float some_function(...) { }
};
?
I emphasized the "public" keyword, because this will be the only real difference in the code. It means that you're explicitly saying that the data are public instead of relying on the struct keyword (which would have made them implicitly public). I tend to find that more awful than having member functions in a struct (which I don't find that awful anyway).

I fail to get it [smile]

Share this post


Link to post
Share on other sites
I agree. I always use the struct keyword because I always need a public section.

Share this post


Link to post
Share on other sites
I'm sure you've noted this already, but the convention is:

If it contains only data: Use a struct
If it contains data and functions: Use a class

This convention is funny of course because there is no real difference between a struct and a class.

[Edited by - fpsgamer on August 15, 2007 10:58:17 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Glak
that's YOUR convention. I personally think that it is a bad one.


Thanks for crediting it to me, but I certainly didn't invent it.[rolleyes]

I'd say its wide spread use came from the sheer fact that many people don't realize that structs can exhibit inheritence, polymorphism, have functions etc.

However this convention does provide the advantage that it makes providing C-linkage easier when you export structs that only contain POD types ... this essentially keeps all structs as C-style structs.

Share this post


Link to post
Share on other sites
Quote:

I always use the struct keyword because I always need a public section.


Quote:

that's YOUR convention. I personally think that it is a bad one.


Well, bad is perhaps too strong a word. I just wouldn't use it. Like many people, I prefer using class to denote non PODs, and structs to represent PODs, things that act like PODs (like 2 dimension math vectors) and functors.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!