Class vs. Structure?

Started by
10 comments, last by Zahlman 16 years, 8 months ago
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.
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.
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
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.
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]
I agree. I always use the struct keyword because I always need a public section.
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]
that's YOUR convention. I personally think that it is a bad one.
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.
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.

This topic is closed to new replies.

Advertisement