struct vs classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

struct st_one{
public:
	void set(int x){hello_struct = x;};
	int get(){return hello_struct;};
	st_one(){hello = 0;};
	
private:
	int hello_struct;
};

class cl_one{
public:
	void set(int x){hello_class = x;};
	int get(){return hello_class;};
	cl_one(){hello_class = 0;};
	
private:
	int hello_class;
};

int main()
{
	st_one _struct;
	cl_one _class
	
	_struct.set(2);
	_class.set(2);
	
	std::cout << _struct.get() << std::endl;
	std::cout << _class.get() << std::endl;
	
}


I've always thought about how other programmers use classes or struct if they want to define their own type, provided they explicitly (not implicitly) make members public or private. I'd prefer to use classes if I'm using public member function with private variables, and just leave struct only for public variables.

Thoughts?

EDIT:: accidentally left "cl_one" declared as a struct, instead of a class.

Last edited on
If its for somthing quite simple, i.e. a few variables, I would use a struct, otherwise I would use a class if it has functions etc. If you have a function to call and you find you are passing say 6 arguments I would replace that with a struct, then you needn't change the function signature but only the struct. It's all down to a matter of taste. I also like to keep my code looking good, if your code starts looking messy somthing is wrong somwhere, but I am now digressing...

HTH
closed account (o3hC5Di1)
Hi there,

The only difference between struct and class to a compiler is that by default, without you stating so, struct members are public, whilst class members are private:

1
2
3
4
5
6
7
8
9
struct my_struct
{
    int member; //this is public
};

class my_class
{
    int member; //this is private
};


Other than that, they are the same. Structs are a C-feature and in C you are not allowed to define member functions as in C++. So I like to use structs for simple type which only have data members. The only member functions I might define are overloaded operators, if applicable.

Hope that helps.

All the best,
NwN
NwN is right.

However I am used to use "struct" only when the the usage of "struct" would be also legal in C. So I usually use the keyword "struct" to identify a simple data structure (all public variables, no functions).... except some very rare situations where I decide to make an exception.

In the other cases I prefer to use the keyword "class".

It is my own personal style.... but, like NwN said, in C++ there is no an actual difference from "struct" and "class" (a part the difference of "public as default" vs "private as default") so you are absoultely free to use one keyword or another indifferently
Last edited on
Unless I am required to by a style guide, I never use the keyword "class". It's just personal preference (I like being rebellious by using "struct" for high-level C++ classes). So really you can do whatever you want.

I recommend sticking to one style though - decide when you will use class or struct to define a class and stick to that definition unless a group or work project requires otherwise.
Basically, the convention of leaving only data members for "struct" is a throwback to C language
Last edited on
Topic archived. No new replies allowed.