简体   繁体   中英

Advantages/Disadvantages of a Inheritance, composition and multiple member variables

I am looking at Ogre3D code and WildMagic code and I found that both deal with their core classes a bit differently. Since I am creating my own core, I was wondering which would be better practice and could potentially be better in terms of resources.

In WildMagic, there is a Matrix class which inherits from a Table class (not polymorphic). The table class can have N row and N cols and gets getters and setters for the columns and rows. The Matrix class then has functionality that only makes sense for a Matrix and it conveniently inherits from Table. A vector can then also inherit from Table in this case (although WildMagic does not do it). WildMagic also has a Transform object that stores local and derived transformations for a Node. So a Node will have two Transform objects that contain the necessary transformations (which include position, rotation, scale).

In Ogre3D on the other hand, the Matrix class does not derive from anything and Ogre3D's node has variables to store the local and derived positions: Vector localPos; Vector derPos; Matrix localRot; Matrix derRot; etc. Vector localPos; Vector derPos; Matrix localRot; Matrix derRot; etc.

Now keep in mind these are core objects that will be used/updated/modified thousands of times per frame and it is quite hard to change them if you realize you have a performance bottleneck down the road when you have a full game relying on these core classes.

So now questions are:

  1. Is there a cost associated with a Matrix class inheriting from a Table class vs a Matrix class doing everything in the first place
  2. Is there a cost associated with having 3 instance of a Transform Object in a Node class vs exposing the underlying datatypes via member variables (I realize that in both cases, it is composition, just that in one case, there a wrapper for transformations while in the other case, the transformations are exposed directly. I guess the question can be rephrased to: Does a wrapper have a cost (considering it is an object that needs to be created and destroyed)?
  3. Let's assume that I am writing an application where every calculation (even a simple Vector addition) counts. Would you change your choice?

Very good, complicated questions with many, many answers. I'll try to address them as best I can, but at least in my opinion the overall answer is going to do more with what your needs are than what a single individual might do.

1) There is always a cost to inheritance, though it is very minute. There really isn't an easy answer to this question so I would probably read: http://www.hackcraft.net/cpp/templateInheritance/

2) Anything that has to be created or destroyed has a cost. Generally wrapping things makes them "safer" for a large number of people on a project, but otherwise I don't find it useful or worth it. Just like inheritance the cost of wrapping things is very small.

3) If every calculation matters, than don't wrap things if you don't have to. And at least for simple math classes just use templates for ease of use instead of inheritance.

Overall for basic data structures for matrices and vectors you aren't going to kill your program if its on PC or a console with either approach. A slow memory manager and memory allocations will kill your game a lot more than inheriting for your vector and matrix classes.

If you're not making virtual function calls, then it won't matter a shred from a pure performance perspective. I personally would disfavour inheritance over any other solution. Small wrapper objects will be optimized out by the compiler. However, I wouldn't exactly encourage you to look at OGRE3D as a pinnacle of good design- they're rife with Singletons.

For (1), are you positive there is no polymorphism? Virtual function calls are the biggest efficiency hit when it comes to inheritance. If there are no virtual functions, there will be almost no difference between the two, and definitely no difference that can be generalized. Maybe the objects will be laid out differently in memory in one case versus the other, maybe you'll have different sizes, with more wasted buffers in one, that sort of thing.

For (2), it all comes down to how likely the code is to be optimized. Generally, if the relevant functions are all easily inlined, there won't be many actual function calls in the lightweight wrapper case. But again, there could be consequences for things like memory layout. Its difficult to predict in the abstract.

For (3), I haven't made a choice, so I don't know what you're refering to:-P

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM