Structs vs Classes??

Started by
33 comments, last by RedGenie 21 years, 5 months ago
Does anybody have any ideas on the pros and cons of using Classes against Structs partivularly from a performance point of view. Lets say ive got 100 characters and I increment their position within a loop. The Class way:
  
///////////////////////////

//Character Class (CCharacter)

///////////////////////////

POINT charPosition;

POINT GetPosition() {
  return charPosition;
}

void SetPosition(POINT newPosition) {
  charPosition=newPosition;
}


///////////////////////////

// Main program

///////////////////////////


CCharacter aCharacter[100];

for(int characterCount=0;characterCount<100;characterCount++) {
  int newxPos=aCharacter[characterCount].GetPosition().x+1;
  int newyPos=aCharacter[characterCount].GetPosition().y+1;
  aCharacter[characterCount].SetPosition({newxPos,newyPos});
}
  
And the struct way:
  
///////////////////////////

// Main program

///////////////////////////

struct {
  POINT charPosition;
} CHARACTER;


CHARACTER aCharacter[100];

for(int characterCount=0;characterCount<100;characterCount++) {
  int newxPos=aCharacter[characterCount].charPosition.x+1;
  int newyPos=aCharacter[characterCount].charPosition.y+1;
  aCharacter[characterCount].charPosition={newxPos,newyPos};
}
  
Would the struct way be significantly faster? Probably not for 100 characters, but lets say 2000. This is simplified code, the character class would obviously contain much more functionality but im just wanting to know if there is any significant overhead in having the code class based. (Or is a struct basically a class anyway in which case I guess there would be no difference).
Advertisement
The only difference between a struct and class is that in structs everything''s public by default and in a class everything''s private by default.

A CRPG in development...

Need help? Well, go FAQ yourself.

Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Um...I don't know much about OO...but given your example...the struct way should be faster

I'm baseing this on the fact that in the class basied example you are changeing the position by useing a function...which would require some extra work with the stack...pushing/poping variables, jump calls, etc...plus the extra function calls to return the variable values in the class.

In the struct you are changeing them directly (no function calls).

but then I don't know how the compiler deals with classes and such....

Hmm...now that I think about how does the compiler deal with the data in a class?

I meen in the struct you have the bennefit of ajacent memory storage because of the array used...so there is a potential speed increase from useing the cache...and because you are increaseing both X and Y by 1 you could make the routine even faster by dropping the need to temporarily store a integer X and Y value and just use the CPU registers instead...


[edited by - MSW on October 21, 2002 12:37:56 AM]
quote:Original post by RedGenie
Does anybody have any ideas on the pros and cons of using Classes against Structs partivularly from a performance point of view.

Structs are classes in C++, which means that your "struct" example isn''t the C++ use of them, which negates the comparison.
You''re thinking too much. It''s not Structs vs Classes. It''s Structs and Classes. Use them where it is appropriate (although c only lets you use structs and c++ rhetoric says ONLY use classes for EVERYTHING). When something needs to be protected or inheritance, polymorphism, etc.., use classes, when something needs quick excessabilty with minimal protection use structs.

"Love all, trust a few. Do wrong to none." - Shakespeare

Dirge - Aurelio Reis
www.CodeFortress.com
Current Causes:
Nissan sues Nissan
"Artificial Intelligence: the art of making computers that behave like the ones in movies."www.CodeFortress.com
For that specific purpose it is often faster, unless the class''s function is inlined. In an optimised build you are better off profiling it or looking at the assembly output manually if you are picky on performance. I doubt its anything but marginal however.
For that specific purpose the struct is often faster, unless the class''s function is inlined. In an optimised build you are better off profiling it or looking at the assembly output manually if you are picky on performance. I doubt its anything but marginal however.

.. from what i've heard functions in a class are automatically inline functions. so the class might run at the same speed (or a little bit faster) as the struct version.

of course i'm a beginner in OO so i'm guessing.
if i'm wrong, they roast (read: flame) me and correct me anyway.

[edited by MKV]



[edited by - MadKeithV on October 22, 2002 7:27:30 AM]

Beginner in Game Development?  Read here. And read here.

 

quote:Original post by Dirge
c++ rhetoric says ONLY use classes for EVERYTHING


In C++, a struct IS a class, with the ONLY standard difference being that the default access specifier in a struct is public, and in a class private.

It has already been said in this thread too, but apparently, reading is not a requirement for responding anymore.

MSW - there is a potential function call involved with using a function-based modification, yes, but for most short functions the compiler will try to inline it, negating the difference. The keyword 'inline' can be added to demonstrate that you know the function-call overhead might prove a bother, but in the end it's still up to the compiler to decide if it can or cannot inline the function. Usually it does, for short functions like in the example.

[edit: for functions that have their bodies within the class specification (i.e. usually in the header file i.s.o. the CPP file), inline is implied, meaning the compiler will try hard to remove any function-call overhead and some temporary-object construction overhead]



[edited by - MadKeithV on October 22, 2002 5:34:28 AM]

[edited by - MadKeithV on October 22, 2002 8:29:42 AM]
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
On that note, any eta on the search function?


[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people
[size=2]

This topic is closed to new replies.

Advertisement