Sign in to follow this  
KSP

Class vs. Structure?

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
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'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
Quote:
Original post by fpsgamer
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.


That is ONE of about 3 common conventions.

Convention 1 - Stated above, struct = data only, class = data + functions.

this is the most common one for C programmer's turned C++, but not the most common for C++ in general. (based on my admittedly unscientific data sample)

Convention 2 - struct = class with public access directly to data, class = class without private or protected data.

This is more common than convention 1 for pure C++ programmers.

Convention 3 - struct for class with public data and POD semantics for copy-construction / assignment. class for anything that does not duplicate POD semantics.

Also popular with modern C++ people.

Convention 4 - struct or class considered completely equivelent, and not differentiated by any special convention. As a consequence either: 4a) only 1 of the 2 is ever used, or 4b) which is chosen is based solely on initial access section and-or inheiritance specifier.

This is also quite popular with true modern C++ programmers (those who either learned since the C++ standard, or keep informed and up-to-date with developments and standard's compliance) - but mostly unpopular with those from other backgrounds (like C#, C, Java, etc)

Share this post


Link to post
Share on other sites
Recently I only find myself using struct. Why? Well, it's convention 4 with the justification that I want to write the interface first (where it's seen right away) and implementation details after (where they're more ignorable), so I therefore want a default of public access. I'm responsible enough to write 'private:' if there *is* anything should be private, so I don't need or want the language's attempt to check up on me. (This is different from "not needing or wanting", say, standard library components, because with private vs. public, it actually does save effort - however tiny.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this