Advertisement Jump to content
Sign in to follow this  
cozzie

Struct versus class?

This topic is 1815 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,

 

Just wondering. What do you see as the 'boundary' when you prefer a class versus a struct?

Was thinking about this when I was adding functions to my VECTOR3 struct, the number of operators has grown, and I'm thinking of also adding (inline) functions for cross- and dot- product. Included are already getting the magnitude and normalizing.

 

For now I don't see reasons to move from a struct to a class.

Operators work nice and do exactly what I want without to much code (not even sure how to do that within a class.....)

 

Somehow my mind says, as long as it's a relatively 'simple' object, just use a struct and don't 'waste classes' on it (not sure though how to explain this rationally :))

 

What are your thoughts?

Share this post


Link to post
Share on other sites
Advertisement

Thanks, that's a nice addition. In my vector struct indeed the members are all accessible (and should be).

Would you add for example a function in the struct to calculate the cross or dot product? Or would you add 'Global functions' (yikes!! smile.png) in the same header/ cpp files

Edited by cozzie

Share this post


Link to post
Share on other sites

As frob mentioned, in C++ a class and a struct are pretty much identical. The choice of which to use is yours to make, seeing as the following two are very much the same:

struct Vec
{
   float x, y, z;
};

class Vec
{
public:
   float x, y, z;
};

One issue i ran into when i used a mix of structs and classes was when forward declaring them. Sometimes i would simply forget if Vec was a class or a struct. Similar with Color, Rect, Transform, etc. So i settled on using classes only, and if something needs to have all of it's members public, i simply set them as public and not protected/private.

Share this post


Link to post
Share on other sites

Actually they are not pretty much identical they are identical, the code generated from them is exactly the same, a struct can have all the same things as a class.

 

The only difference is semantical regarding the default access rule for members (public vs private), which is why i said "pretty much identical", but yeah, the generated bytecode is the same.

Share this post


Link to post
Share on other sites

One thing I like to do with C++ is use structs as classes, but hide the class related code when C is accessing it.  This came in handy a few times for me.

struct whatever_t
{
#ifdef __cplusplus
public:
  void func1();
  int  func2(void*);
  byte func3(char, float);

public:
#else
//  struct _vtbl
//  {
    void (*func1)(struct whatever_t* This);
    void (*func2)(struct whatever_t* This, void*);
    void (*func3)(struct whatever_t* This, char, float);
//  }*vtbl;
#endif
  int var1, var2, var3;
};

#ifndef __cplusplus
 void whatever_func1(struct whatever_t* This);
 void whatever_func2(struct whatever_t* This, void*);
 void whatever_func3(struct whatever_t* This, char, float);
#endif

I once needed the functionality of classes, but at the same time, needed compatibility with C and C++.  This is how I did it.

 

EDIT: Minus the vtable.

Edited by blueshogun96

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!