What's better? objects or plain functions

Started by
3 comments, last by snk_kid 19 years, 8 months ago
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
~Bolt"All men dream: but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity: but the dreamers of the day are dangerous men, for they may act their dreams with open eyes, to make it possible." This I did...
Advertisement
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?
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.
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;

}




~Bolt"All men dream: but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity: but the dreamers of the day are dangerous men, for they may act their dreams with open eyes, to make it possible." This I did...
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.

This topic is closed to new replies.

Advertisement