# Struct versus class?

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

## 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 :))

##### Share on other sites

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!! ) in the same header/ cpp files

Edited by cozzie

##### 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 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 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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 18
• 33
• 9
• 16
• 22