Classes vs Functions
Lately I have been trying to figure out the
best way to design a 3d engine, and I have
been absorbing as much literature about code
architecture as I can. However, there seems
to be two popular approaches that I can''t
decide between. The first approach is to
use C++ classes to represent everything.
I see this approach used in books like Design
Patterns and Large-Scale C++ Software Design,
and also it appears this is the approach used
by the Unreal engine. The second approach
is to divide everything into object-oriented
components like in the above approach, but
to use C prefixed functions (like VID_Init())
for the interface instead of classes, and
only use classes when there is a specific
use (operator overloading, the constructor,
etc). I believe this approach is advocated
by Chris Hargrove in Code on the Cob,
Corrinne Yu (from a recent slashdot posting
I saw), and the Quake engine. I''m not sure
which approach is better, so I was wondering
if you all could tell me what your take on
this is. Thanks in advance for any thoughtful
answers!
-----Blah
You are debating about form, not function. While a debate about good form or the best style has a place, I do not believe you need to worry so much about this issue. Using structs and C style functions with prefaced names is EXACTLY the same at run time as using a C++ class with a member function ... ASSUMING the following is true - that the function actually needs to operate on the data of the associated class/struct. I would say the quake engine uses the prefaced function style probably because they code (or at least coded) in C, not C++, because C was far more stable and portable when Quake was originally written (and they were experienced with it). They might also have decided that since they were going for ideal code generation - not ideal architechture development - then the C style version allows your form to remain the same when you elliminate access function calls, etc, and allows you to optimize memory allocation and such a little more easily than C++ (because C++ imposes stricter rules in certain areas). Basically, my opinion is this: if you and your teamates know C++ and like the Object Oriented Paradigm, use C++ and classes - It''s easier to track changes to an objects interface and MAKE SURE everyone is doing everything right. But if anyone is uncomfortable with the dark corners of C++ or advanced OO design, go with the C solution.
Well, this is actually a debate on using Structured design techniques vs. Object Oriented design techniques. There''s really no agreed upon answer either. In reality, it really doesn''t matter. What ever you like better is probably what you should do. Also, don''t forget that it is possible to combine these techniques.
There''s not really any real difference between C and C++ as far as performance goes. Well written C++ code will run circles around poorly written C code, and vise versa.
There''s not really any real difference between C and C++ as far as performance goes. Well written C++ code will run circles around poorly written C code, and vise versa.
exactly, the most important thing to remember is what you pay for (in terms of resource usage) for C++''s features, and whether or not you''re getting enough bang for your buck.
Most new C++ programmers assume C++ is slow because they''re cavalier about using virtual functions, RTTI, multiple inheritance, etc.
Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Most new C++ programmers assume C++ is slow because they''re cavalier about using virtual functions, RTTI, multiple inheritance, etc.
Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
Stupid but practical reason to use a class heirarchy:
VC++ autocomplete
No more having to look at header files to see what a function was called.
VC++ autocomplete
No more having to look at header files to see what a function was called.
-the logistical one-http://members.bellatlantic.net/~olsongt
I think in some instances. just wrapping code can give it a performance hit, but like Mason said, it has something to do with exactly how you chose to wrap it. Personally, I llok at it like thise, if you want it to run as fast as possible, you may want straight C, however, if you want ease of use and encapsulation, use C++. I perfer the later myself, I''d like to be able to use the libs I create at somepoint in life. I find classes much cleaner (IMHO) and the interface makes much more sense to me. Further, autocomplete is a pretty nifty feature. Score one for laziness.
Write more poetry.http://www.Me-Zine.org
Use C++. There''s plenty of speed here if used correctly.
inline functions are great speed helpers, and the use of
classes helps keep things straight. If using methods on C structures, you''ll still have to use a pointer to the structure that the method is working on. C++ passes the THIS pointer automatically so you save on typing and development time.
inline functions are great speed helpers, and the use of
classes helps keep things straight. If using methods on C structures, you''ll still have to use a pointer to the structure that the method is working on. C++ passes the THIS pointer automatically so you save on typing and development time.
--Shannon Schlomer, BLAZE Technologies, Inc.
WHY do people keep saying that C is faster then C++? I''ve done test in the past on this and I''ve found that a program written in C++ using classes runs the same speed as a program in C using only functions if the code does the same thing. The compiler should compile the C++ class functions into the same assembly code as the C functions.
I''m currently writting a 3D engine myself and I''m using classes for it. An object is a class and contains parts which contains points, splines, regions, etc. When I want the object to redraw, I just type "mychar(x).redraw" and it''ll send the redraw to it''s parts and so on.
I think writting functions with prefixed names could get messy looking after a while, but they both should compile the same way.
E:cb woof!
I''m currently writting a 3D engine myself and I''m using classes for it. An object is a class and contains parts which contains points, splines, regions, etc. When I want the object to redraw, I just type "mychar(x).redraw" and it''ll send the redraw to it''s parts and so on.
I think writting functions with prefixed names could get messy looking after a while, but they both should compile the same way.
E:cb woof!
E:cb woof!
The reason most people say this is because it has only been recently that C++ compilers have caught up with the best C compilers out there. Traditionally C ran faster than C++ and to this day C has less overhead than C++ so it has performance advantages. In theory C is faster than C++ because calls in C++ eventually end up calling C libraries. Think of the string libraries. strcpy is close to the lowest level usable C function. Sprintf(printf, fprintf) are the next lowest level because these functions format and then call strcpy. Most String classes use a combination of sprintf and strcpy to accomplish its goals. By sheer analysis of what this means in operating system stack usage, you could surmise that a string class has to do 2-3 times the work as a straight strcpy to accomplish the same feat. Most modern compilers can see through this and because compilers can see through it, C++ is becomming a faster language everday. So in a sense this is why many people still consider C++ slower than C.
Kressilac
Kressilac
Derek Licciardi (Kressilac)Elysian Productions Inc.
Still, even older compilers should see "object::redraw" the same as "object_redraw". Putting it in a class shouldn''t make it compile any differently. Plus, most C++ commands should compile strait to assembly. There''s no reason a command line "cin" should compile to "printf" first, then compile to assembly.
Then again, I did say "should" and we all know that a lot of programs out there are not written all that well. I personaly use CodeWarrior for all my programming (an old version though) and I''ve found it to be one of the better ones. I use to use Think C++ before that and even it compiled the same as Think C, which I also had.
E:cb woof!
Then again, I did say "should" and we all know that a lot of programs out there are not written all that well. I personaly use CodeWarrior for all my programming (an old version though) and I''ve found it to be one of the better ones. I use to use Think C++ before that and even it compiled the same as Think C, which I also had.
E:cb woof!
E:cb woof!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement