C++, structs vs classes/objects

Started by
24 comments, last by Polymorphic OOP 19 years, 2 months ago
Quote:One more thing to meantion is most people will use a Set___ and get___ function calls to tranform a open struct to a much more protected Class, so it's really up to you apparently.


And they'd be wrong. If you need that level of access to the class members, then it's just a glorified struct.

**Cough**. [grin]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Quote:Original post by Oberon_Command
Quote:Original post by Tsuki no Hikari
They aren't the same structs as in C.


Also, in C++, when you declare structs like this
struct Foo {
int x;
int y;
int floofy;
}



you don't have to declare variables like this:

struct Foo this_foo;

You can just do it like this:
Foo this_foo;

You would not be able to do that in C.


In C:

typedef struct FooType {int x;int y;int z;} Foo;Foo this_foo; // Works in both C AND C++ :D


This method is also perfectly compatible with C++ so I generally use it regardless of whether or not I'm using C, just for compatibilities sake if I ever want to pull the struct into a C project.

throw table_exception("(? ???)? ? ???");

Quote:Original post by Fruny
Quote:One more thing to meantion is most people will use a Set___ and get___ function calls to tranform a open struct to a much more protected Class, so it's really up to you apparently.


And they'd be wrong. If you need that level of access to the class members, then it's just a glorified struct.

**Cough**. [grin]


Depends. The Set() methods could be clamping values to the appropriate bounds and/or performing assertions.

I'll grant you that its still a glorified struct, but its a perfectly legitimate useage :)


[edit] I must say though, thats some impressive template work you linked to Fruny. Very interesting, did you develop that on your own or did you pick that up somewhere?

[Edited by - Ravyne on February 14, 2005 9:22:11 PM]

throw table_exception("(? ???)? ? ???");

well i agree that in structures we usually don't have methods except for constructor and destructors

i saw many things like :

struct Blah
{
...
...
anything* pthing;
~Blah() {if (pthing) {delete[] pthing; pthing=0;}}
}

int the way i use them, the main difference between struct and class is that i dont put methods in struct (except destructors) and i don't put a "m_" prefix on structure's variables.
Tchou kanaky ! tchou !
Quote:Original post by Mawww
well i agree that in structures we usually don't have methods except for constructor and destructors

If you have to have a destructor then that implies (to me) that the struct is responsible for looking after some important data or resources etc. It would make sense to make the data private.

In C++, structs can do everything a class can do. However people often use structs to bunch data together but which doesn't have any functionality or responsibilities. Once you start getting them (responsibilities that is), people would usually use a class. This is a style and not a neccessity.

I often use structs for things like functors, which aren't passed around or don't have any important data, or in example code on gamedev, but it is simply because it means I have to finger type less and it makes the examples shorter (not littered with public, private etc). e.g.
struct RenderThing {    RenderThing(Renderer& renderer) : m_renderer(renderer) {    }    void operator()(const Thing& thing) {        thing.Render(m_renderer);    }    Renderer& m_renderer;};
I usually only use structs for "helper" types which contain no data. By that I mean any types which I just use for private inheritance (usually with CRTP) that are only there for providing common functionality to a class. Aside from that, any metafunctions, metaclasses, and similar, I prefer to create as template structs. It's all mostly just a matter of taste, but if you don't stay consistent, it can be very annoying in the long run (i.e. if someone needs to make your type a friend, they have to know if you made it a class or a struct [or a union] -- being inconsistent with what you make structs and classes only makes that more difficult).

This topic is closed to new replies.

Advertisement