Archived

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

karan13

Programming Question

Recommended Posts

Programming Wise which is better... I want to also save and load the data, as well as manipulate it, so which programming way is correct... Thanks.. struct ShipData { char *m_cShipName; //The name of the ship //Hull Information char m_cShipHull; }; class cShip { //Declare a member of the pre defined structure ShipData m_Ship; public: cShip(); ~cShip(); }; --- or --- class cShip { char *m_cShipName; //The name of the ship //Hull Information char m_cShipHull; public: cShip(); ~cShip(); }; Karan

Share this post


Link to post
Share on other sites
You should only associate members of a class in a subclass (or struct) if they are very strongly related to each other. for instance, if you have a class Customer that stores information about a customer, you might aggregate the fields relating to his address into an Address struct. In this case, I suggest you not use the struct.

Share this post


Link to post
Share on other sites
For loading and saving data, the first way is easier, because you just call a single write to write the whole structure.

The second way makes for cleaner code, ''cause then you don''t have this->that.theotherthing.

NOTE: What might work with the second way is write(sizeof(cShip),this), which will only write class global data.

---
Make it work.
Make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

Share this post


Link to post
Share on other sites
Perhaps you should look at the larger picture. If the struct is going to be reused throughout the code, use the struct. If not, dont add depths to classes you dont need to, it just makes the coding and debugging harder down the line.

If you are going to reuse the struct I would think about making it a class and adding member functions to that class etc.

Have fun.

Regards

BaelWrath

If it is not nailed down it''s mine and if I can prise it loose,
it''s not nailed down!

Share this post


Link to post
Share on other sites
quote:
Original post by CaptainJester
For loading and saving data, the first way is easier, because you just call a single write to write the whole structure.


Huh? If you write the structure in one call, like WriteFile you only write the pointer to the ShipName not the Shipname itself. And the pointer would be invalid with the next ReadFile call.
If ShipData would be

struct ShipData
{
char m_cShipName[64];
char m_cShipHull;
};
This would work like you said





---------------------------
I may be getting older, but I refuse to grow up

Share this post


Link to post
Share on other sites
You pass the address of the structure to the write function, then it takes everything in the structure and writes it to a file. Then with the read, you have to allocate the structure first, then read the file into the structure. This works when you use binary file access.

Here is a sample for reading a binary file.

  
//Here is the structure

typedef struct X_FILE_HEADER_TYP {
char magic[4];
char major[2];
char minor[2];
char format[4];
char floatSize[4];
} X_FILE_HEADER;

//here is the structure variable.

X_FILE_HEADER header;

//here is the read portion

if((in=fopen(file,"rb"))==NULL) {
return false;
}
fread(&header,sizeof(X_FILE_HEADER),1,in);


---
Make it work.
Make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

Share this post


Link to post
Share on other sites
quote:
Original post by karan13
Programming Wise which is better... I want to also save and load the data, as well as manipulate it, so which programming way is correct... Thanks..



If you are asking about the performance, then i would say that the second one is faster... and is reasonable y would you struct your data if they are used in a class? just declare them in the class.

As was mentioned this->thething is faster than this->that.thething

nevermind...

[edited by - DAHER on July 18, 2002 4:33:26 AM]

Share this post


Link to post
Share on other sites
quote:

As was mentioned this->thething is faster than this->that.thething


Actually, they''re both the same. One pointer dereference and one add.

this->thething:
"this" is a pointer, "thething" is a variable, which is x bytes from the start of the class "this".

this->that.thething:
"this" is a pointer, "that" is a struct, "thething" is a variable x bytes from the start of the struct.
If the struct is 4 bytes from the start of "this" and the variable is 8 bytes from the start of the struct, then the compiler will add 4 and 8 = 12, and add that to "this". The size of structs are known, so this can be optimized.

Back on topic, personally i''d not use structs unless several classes use the same set of data, or it *all* needs to be interchanged (even then, you could still just pass the class itself).

HTH, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
And BTW, try to avoid thigns like:

ZeroMemory(this,sizeof(SomeClass));

Why? Because, as i found out (the hard way, after several hours of debugging), if you use virtual functions, the vtable is included in sizeof(SomeClass), so you end up nulling the vtable, and the app crashes with an access violation whenever you call a virtual function.

2p
Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You are allowed to zero the class members using ZeroMemory (or more portably, memset) in a constructor though aren''t you, the vtable is initialised implicitly as the ctor completes?
Or am I wrong about this?

CObject does similar stuff in MFC I seem to recall, and it''s a good way of guaranteeing zero-initialisation of members when allocated off the heap/free store.

Share this post


Link to post
Share on other sites
My exact code was:

  
CRender::CRender()
{
ZeroMemory(this,sizeof(CRender));
}

But this zeroed out the vtable. I think the vtable is created as soon as the object is created (before the constructor is called), so that virtual functions work in the constructor.

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
You are allowed to zero the class members using ZeroMemory (or more portably, memset) in a constructor though aren''t you, the vtable is initialised implicitly as the ctor completes?
Or am I wrong about this?

CObject does similar stuff in MFC I seem to recall, and it''s a good way of guaranteeing zero-initialisation of members when allocated off the heap/free store.


No, the vtable is set before the ctor begins. Otherwise, you wouldn''t be able to call virtual functions from the ctor.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
No, the vtable is set before the ctor begins. Otherwise, you wouldn''t be able to call virtual functions from the ctor.

You''re not allowed to call virtual functions from the ctor. The behavior is undefined.




"The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your fucking mouth shut and hadn''t asked any questions."
Frank Zappa

Share this post


Link to post
Share on other sites