Advertisement

What are the advans/disadvans of using class member functions vs normal functions?

Started by April 19, 2000 08:22 AM
20 comments, last by Possibility 24 years, 7 months ago
I just learned how to use classes and their member functions, and I was just wondering if they are slower or faster then normal functions and if they have other advantages, and what about class arrays? for example: class TIME{ public: ..... private: ..... } time[5]; are the member functions in there gonna be just as fast as if I were to use a struct and just write my own normal functions? Possibility
There might be a slight performance difference due to implementation details, but there is no reason why a member function should be any faster/slower than a normal function.

Certainly if there is a difference, it will not be very significant, at most the cost of pushing a few more things onto the stack in the function call.

#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Advertisement
As long as you don''t use virtual member methods and/or virtual inheritance, member methods are as fast as normal C functions.

The only difference is that member methods internally have one (implicit) parameter: the this pointer. The value of the this pointer is pushed to the stack prior to calling the member function.

int A::b() { /* ... */; return 0; }
is roughly implemented as
int A__b(const A *this) { /* ... */; return 0; }

However, if you''d try to achieve encapsulation in C, you''d probably end up doing it the same way.

Erik
Don''t let this performance hit discourage you.. Lots of games use OO techniques (Half-Life, etc.) and I, for one, think this is one of the waves of the future in game design.

Just my opinion, though I know others share it.

------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge
I agree - OO is the way to go. Many good things have caused slight performance hits but resulted in massive productivity improvements and algorithmic improvements.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
What is OO? is that object oriented?

Possibility
Advertisement
Yup,
OO is shorthand for Object Oriented. (and OOP is Object Oriented Programming).

-Brian
So what does Object oriented programming actually mean. I am fairly certain I already do that, but what is it precisely and why is SO good?

Possibility
I have programmed in C++ for about 4 months now, and have pondered the exact same thing... anyone?

GO LEAFS GO!
3D Math- The type of mathematics that'll put hair on your chest!
I have been programming in C++ for a few months now, and, although I have learnt and can understand Classes and OOP, I have never actually used them in a project, as I''ve never really seen the need. I never did understand what the advantages/disadvantages were.

This topic is closed to new replies.

Advertisement