1

There's something weird happening in my code these days. Each time I have to built a new member function, or each time I come back to a previously defined member function, I think:

"hey, this is gonna be the exact same process no matter which instance of this class is calling it, so let's make it static!"

and I also feel like it'll be less memory-consuming since each instance won't have to "carry" one implementation of the function, is that correct?

So instead of having things like:

class Foo
{
    int _attributes;

    double methods(int parameters)
    {
        // do stuff using..
    _attributes;

    };
};

I end up with things like:

class Foo
{
    int _attributes;

    static double methods(int parameters, Foo* instance)
    {
        // do stuff using..
    instance->_attributes;

    };
};

And I can't see any function that wouldn't be transformed this way anymore. All my functions are turning to static and I feel like there's something wrong somewhere..

What am I missing? What is the use of having non-static methods, again? XD

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
iago-lito
  • 3,098
  • 3
  • 29
  • 54
  • 10
    *"and I also feel like it'll be less memory-consuming since each instance won't have to "carry" one implementation of the function, is that correct?"* No, it's not. Objects don't "carry" their functions with them. Instead, a hidden `this` pointer is passed as a parameter when you call it - pretty much the same thing as your `static` functions with `instance` parameter, in principle. – jrok Jun 08 '14 at 07:15
  • Ok, so I'm actually just kind of reimplementing this hidden `this` by myself and this is pointless, right? :P – iago-lito Jun 08 '14 at 07:19
  • 1
    You should resort to using static functions only when instance variables are not necessary, otherwise you're not saving anything in terms of efficiency, as jrok pointed out, and just making your code harder to read. As a general rule of thumb, static functions are used for class rules/behavior/algorithms, not anything related to instances. – Allen G Jun 08 '14 at 07:21
  • 2
    I think every beginner programmer eventualy starts to wonder "where and how do objects store their functions?" when they start with OOP. I sure did, too :) – jrok Jun 08 '14 at 07:22
  • Well I just did. Crucial point after all, isn't it? ^ ^ – iago-lito Jun 08 '14 at 07:26
  • 1
    There is actually one case where a class carries its functions with it- in the case of virtual functions. Then it has to have 1 function pointer for each virtual function. Which is cheap enough you can pretty much ignore it, and you generally don't make functions virtual unless you need them to be anyway. – Gabe Sechan Jun 08 '14 at 07:26
  • 1
    You've actually reached a point I did years ago by coming across this. I think of the instance left of the dot as just another parameter except that it lets the method peak at it's privates. Your static method does the same only because, well, it's the same class. Not having to visually check that it's the same class is why I'd rather read the non static syntax. – candied_orange Jun 08 '14 at 07:29
  • Very true, so okay readability 1st. Jeez, I can't believe how reactive you are guys! Thank you so much! – iago-lito Jun 08 '14 at 07:35
  • Now that you've figured that out it might be worth giving this a read: http://stackoverflow.com/questions/155609/what-is-the-difference-between-a-method-and-a-function – candied_orange Jun 08 '14 at 16:31

2 Answers2

7

Congratulations- you've recreated C object oriented programming. This is exactly how we did OOP in C. In fact, its how C++ does it too. it adds a parameter to every function named this which is pushed with the rest of the parameters. You're doing the same thing explicitly.

Why would you use member functions instead? Readability. myInstance.foo() is a lot more readable than myClass::foo(all, my, parameters, myInstance)

candied_orange
  • 7,036
  • 2
  • 28
  • 62
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Ok great, I was feeling something like that. Now I can't help laughing at myself. :P Cheers jrok & Gabe Sechan! – iago-lito Jun 08 '14 at 07:23
4

When you invoke a non-static function.

Foo foo;
foo.doSomething(ArgType arg1, ArgType2 arg2,...);

This compiles down to.

Foo foo;
doSomething(&foo, ArgType arg1, ArgType2 arg2,...);

I answered a similar question here about Java, but it is the same idea in both languages.

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329