简体   繁体   English

非静态函数c ++还剩下什么?

[英]What's left for non-static functions c++?

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 XD

Congratulations- you've recreated C object oriented programming. 祝贺您-您已经重新创建了C语言的面向对象编程。 This is exactly how we did OOP in C. In fact, its how C++ does it too. 这正是我们在C中进行OOP的方式。实际上,C ++也是如此。 it adds a parameter to every function named this which is pushed with the rest of the parameters. 它向每个名为this函数添加一个参数,该参数与其余参数一起被推送。 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) myInstance.foo()myClass::foo(all, my, parameters, myInstance)更具可读性

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. 在这里回答了有关Java的类似问题,但是两种语言的想法都是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C++ 中使用非静态 class 函数的函数 - using functions of non-static class functions in C++ C ++非静态成员函数的开销 - C++ Non-static member functions overhead 实现在C ++中向其注册非静态成员函数的工厂 - implementing a factory that registers non-static member functions to it in C++ c ++指向非静态成员函数的指针 - c++ pointer to non-static member functions C ++静态非静态 - c++ static non-static 让静态成员函数在 C++ 中调用非静态成员 std::function 的正确方法是什么? - What's the correct way to let the static member function call a non-static member std::function in c++? 为什么C ++中的函数重载解析将静态调用的非静态成员函数考虑在内? - Why does function overload resolution in C++ consider non-static member functions for static calls? 类 C++ 中的多线程非静态 - Multithreading non-static in class C++ C ++中的静态全局标识符和非静态全局标识符有什么区别? - What is the difference between static global and non-static global identifier in C++? 模板朋友功能C ++无效使用非静态数据成员错误 - invalid use of non-static data member error with template friend functions c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM