Sign in to follow this  
Boltimus

What's better? objects or plain functions

Recommended Posts

Boltimus    100
Hello everyone, just curious if anyone has come across something similar to this... I was wondering, what would be better of the two possibilities. I'm writing a library for my personal use and am deciding on how to organize it. Options.... 1.) Just write all things I want to do as functions/macros - or - 2.) Encapsulate the functions I want to write as member functions of a class. Where objects are created and their pointers are used. As far as passing parameters to the functions, that would be taken care of by the object properties, so no parameters would have to be passed, as they would be part of the this-> (implied) propoerties of the object. My question is, when the code is compiled, and you call a member function using the pointer, do all the object properties have to be included in the function call, or just the pertinent properties? Is this a bad scheme?? Thanks!!! ~Bolt

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by Boltimus
What's better? objects or plain functions


neither is better, the best is would be what most naturally fits the solution to the problem domain.

So it would probably end up being a mixture of user-defined types with essential operations & free-standing functions that provide some extended useful operations, partitioned/grouped into logical units (namespaces/packages).

Quote:
Original post by Boltimus
My question is, when the code is compiled, and you call a member function using the pointer, do all the object properties have to be included in the function call, or just the pertinent properties? Is this a bad scheme??


When you say "the pointer" are you talking about the this pointer or a pointer to the type in question?

Share this post


Link to post
Share on other sites
Drazgal    368
Im not quite sure what you mean by point 2, probably my fault as my brain is friend right now.

However if you want an object to encapsulate methods instead of using functions, just make the methods static. Groups functionality together nice and neatly and means you dont have to create objects all over the place to use the routines.

Share this post


Link to post
Share on other sites
Boltimus    100
Quote:
When you say "the pointer" are you talking about the this pointer or a pointer to the type in question?


A pointer to the TYPE in question.

Quote:
Im not quite sure what you mean by point 2, probably my fault as my brain is friend right now.


What I meant was, say for example, if I wanted to write a text object, where all kinds of nice "textual" things I wanted done were in the methods for the object. Now say I wanted to call a function in that object, say func1() which contains 3 arguments, but are not listed in the function definition, because I will get them from the properties of the object. I would then call object->func1(). The code for the function might look like...

int func1(){

return object->a + object->b;

}




Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by Boltimus
Quote:
When you say "the pointer" are you talking about the this pointer or a pointer to the type in question?


A pointer to the TYPE in question.


okay then if i understood you correctly then no, you only need to use the data members you need inside the member function.

Quote:
Original post by Boltimus
Quote:
Im not quite sure what you mean by point 2, probably my fault as my brain is friend right now.


What I meant was, say for example, if I wanted to write a text object, where all kinds of nice "textual" things I wanted done were in the methods for the object. Now say I wanted to call a function in that object, say func1() which contains 3 arguments, but are not listed in the function definition, because I will get them from the properties of the object. I would then call object->func1(). The code for the function might look like...

int func1(){

return object->a + object->b;

}


Thats fine but you would need parameters in a member function if you wonted to change the values of the data members if it has been declared protected/private that is.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this