Archived

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

willow01

OOP features worth using.

Recommended Posts

Hello I was wondering if I could get some direction on using oo features in game programming using c++. Which features specifically inheritance, polymorphism, templated classes, etc. impact on a games performance and how i.e. speed, memory useage, etc. I know it is a broad question but I would like to avoid using some of these things before I get to far into the design of a game and have to re-do my designs later. Also is it a good idea to use stl containers.

Share this post


Link to post
Share on other sites
I think all of the things you mentioned are worth using to an extent, but try not to go crazy with inheritance. It''s probably the most overused (and incorrectly used) feature of OO programming. If you haven''t used OO/C++ much, I''d highly recommend Scott Meyers Effective C++ series and the C++ FAQs book. Both have discussions on how to use inheritance in C++ the right way. In short, prefer abstract bases classes and shallow inheritance hierarchies. Stroustrup also talked about this in a recent article that was mentioned on this site, but I don''t have the link handy.

Re: performance, design your application for you first. Then profile it and tune it for the machine only if you find bottlenecks that need to be tuned.

--
Dave Mikesell Software & Consulting

Share this post


Link to post
Share on other sites
This is a good read. It''s targetted toward C++ on embedded systems. However, I think it''s a great read for people who aren''t yet proficient with C++. By following the rationale they lay out, you can become more familiar with a subset of C++ features (I think the term ''OO features'' is inappropriate for the question) rather than worrying about which features to use when and where. This helps build a strong foundation from which to experiment with other features. Just an opinion

Share this post


Link to post
Share on other sites
Hi,

Most C++ features are frowned upon for game development (well at least at the company I work for), but I manage to sneak in a couple of classes and C++ every now and then.

Inheritance is good for defining objects with similar interfaces.
Beware of using too many new[] and delete[] operators especially if you have code in your constructor/destructor. But in this vain, try and remove dynamic memory allocations and deletions from your gameloop completely. Use structures like freelists which reduce memory pages and memory fragmentation during the game loop.

Polymorphism is slow if you have lots of polymorphic calls in your game loop, although it is useful for some types of tasks such as managing your game objects. Also, we use a lot of polymorphism for our debugging code - things like script objects for setting up levels as well as debugging menus - this stuff gets defined out for our final builds so is not in the final product.

Templated classes are useful for data-structures and for maths functions acting on different types, although the expansion of several data structures and functions for different types may swell memory usage slightly. (Kind of analogous to inlining of functions but with type definitions.)

We personally don''t use STL because we develop for CGN,PS2 and XBOX too, although I have heard it is quite efficient. But like most things in game programming you have to do things in the right way - it''s no good storing a list of triangles in a linked list and then rendering from that. STL may help you with things like implementing a virtual machine or managing game objects or something like that. Does anyone know if STL has a freelist type? (I haven''t used it that much)

Any other professionals have comments?

Share this post


Link to post
Share on other sites
quote:
Original post by FReY
Does anyone know if STL has a freelist type? (I haven''t used it that much)

I don''t even know what a freelist is, but no, the standard library doesn''t contain such a type. It only contains low level general purpose data structures.



[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can of course provide an allocator class as the last template parameter for all STL containers. You could implement a free-list allocator and tell the STL to use that. The default allocator is normally OK though. Check out allocator in your STL docs.

Share this post


Link to post
Share on other sites