Archived

This topic is now archived and is closed to further replies.

bilsa

structures?

Recommended Posts

Hello again I got a couple of questions about structures. I have been reading some about them but haven't found any resource where they have any information that goes "deep"... Well, what is the differense between a structure and a class and when should I use either of them? So far I have only been using classes, since I have never understood why I should use a structure insted? But then when I bought Jim's Programming RPG book, I saw that he uses structures for Vertex information. Why shouldn't I use a class instead? And lets say that I don't fill my structure completely, just use a couple of variables inside it, will the others still consume memory? So, basically I am asking for the pros and cons of a structure VS a class. If anyone has a very informative resource where structures are described I would be very happy if you could share it. Thak you! [edited by - bilsa on May 5, 2003 9:32:09 AM]

Share this post


Link to post
Share on other sites
In C++, the only difference between a class and a struct is that with a struct, the default access class is public, whereas the default for a class is private. Generally, I use a struct if it has public data members, but a class otherwise.

[edit]
Any data member will consume memory, even if you never use it. This is the same whether you use struct or class (because they are essentially the same thing).

[edited by - tr0n on May 5, 2003 9:35:41 AM]

Share this post


Link to post
Share on other sites
>And lets say that I don''t fill my structure completely, just use a couple of variables inside it, will the others still consume memory?

UHmm, yes.Like:

struch MyPocket {
int dollars;
int DanishKroner;
int EnglishPounds;
};

MyPocket pocket1;

pocket1.dollars = 100000; //I wish!

pocket1.dollars++;

--
DanishKroner and EnglishPounds still takes up memory here. On a 32 bit system, an int is 4 bytes: 32 bits. So each instance of MyPocket would use 96 bits - even if you are only using the dollars variable.


Struct vs Class in C++.
Well, the easiest way to use them isto use a struct if there are no functions on it, and a class if the datatype requires functions. That way your code probably would be more readable. But in C++ struct and class are essentially the same except for the default scope. ie

struct bla {
//this is public!
int i;
private:
//this is private
int b;
};

class bla2 {
//this is private!
int i;
public:
//this is public
int b;
};

Share this post


Link to post
Share on other sites