Structure.

Pages: 12
I was looking around in this website structure tutorial and i noticed a similarity between structures and classes, that similarity lead me to this conclusion.

When ever you create a structure all that is required of it is a name to be listed by. And that given name holds information on the allocation of the variable/const that you implement within your structure. A similarity between classes and structure is that they both require the use of objects to be able to access them. Also in order to access the content within structure and classes one must use the object initialized with the combination of DOT 's. Regardless a difference between the two is that structure cannot be implemented from a second hand header file, and unlike structure , classes require classification between public and private. This is based on my understanding. The purpose of this post is to receive corrections on my theory/ way of thinking and hear for other theories and explanations.

As always replies will be appreciated.
Last edited on
The similarity between struct and class is that they're identically. The only difference is the default behavior when it comes to private or public.
struct is public by default while class is private
That makes more sense then my horrific paragraph, thanks.

In that case, then what are struct's good for if it can be replaced by classes?
Last edited on
structs are from C, and C++ only has them for backward compatibility. It really doesn't matter which one you use.
Personally, I use a struct when there's only public variables and one or more constructors. I use a class for anything that has private members or methods.
I use whichever involves less typing:
1
2
3
4
5
class Blah : public Meh
{
public:
    //bleh
};
1
2
3
4
struct Blah : Meh
{
    //bleh
};
And vice-versa. Though I usually prefer to use struct over class, just to be rebellious :)
Last edited on
I don't like structs to have methods, I don't think it makes sense. I only write a constructor because C++ doesn't allow struct coords my_coords = { 0, 1 }; so I have to do coords my_coords(0, 1); instead.
Last edited on
You're mistaken:
http://ideone.com/F2pjw
Not before C++11 he wasn't.

EDIT: Nevermind...
Last edited on
closed account (S6k9GNh0)
I don't believe GCC 4.3 was C++11 compatible. Plus, this syntax was/is used in C as well.
Last edited on
Not before C++11 he wasn't.


No, it works fine in C++03 too.
Yeah, it's been in C++ since the beginning - I don't know why you thought otherwise 0_o
Hmm... I was sure you couldn't do it like that. Never mind.
closed account (1yR4jE8b)
It would *have* to be in C++ from the beginning, otherwise it wouldn't be backwards-compatible with C.
closed account (zb0S216C)
It's common to find structs representing POD (Plain Old Data) types, or to represent something that isn't an object in the real world. classes on the other hand, are used to represent something that would be a physical object in the real world, such as a car.

Wazzak
What place does a union have in C++? It seems strange for them to support member functions and access control: http://ideone.com/BhTSs
I guess the answer to your "why" is "why not". Union is still there because it's still useful (you probably know already but on the off-chance you don't, union's purpose is to store multiple variables in the same location in memory, which has various uses.).
Like what?
If I was writing a dynamically typed language:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
typedef enum __var_type__ {
	VAR_TYPE_INTEGER,
	VAR_TYPE_FLOAT,
	VAR_TYPE_STRING,
	VAR_TYPE_POINTER
} var_type;

typedef struct __var__ {
	char*			name;
	var_type		type;
	union {
		long		as_integer;
		double		as_float;
		char*		as_string;
		unsigned long	as_pointer;
	} value;
} var;

int main()
{
	var foo = { .name = "foo", .type = VAR_TYPE_INTEGER, .value.as_integer = 0 };
	return 0;
}


Or an x86 emulator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
typedef union __register64__ {
	uint64_t	value;
	register32	low;
} register64;

typedef union __register32__ {
	uint32_t	value;
	register16	low;
} register32;

typedef union __register16__ {
	uint16_t	value;
	struct {
		uint8_t	low;
		uint8_t	high;
	} s;
} register16;

...

struct x86_registers {
	register64	rax;
	...
};


Then rax.value is the value of RAX, rax.low.value is the value of EAX, rax.low.low.value is the value of AX, rax.low.low.s.high is the value of AH and rax.low.low.s.low is the value of AL.
closed account (S6k9GNh0)
That's poor use of the union. That's not guaranteed by the standard to be failsafe and is probably compiler specific (for the x86 emulator, the typing use is very common).
Last edited on
Pages: 12