c++: Difference between member and non member functions

前端 未结 6 903
一整个雨季
一整个雨季 2020-12-12 20:35

What is the difference between member and non-member functions in C++?

6条回答
  •  醉话见心
    2020-12-12 21:35

    A (non-static) member function has an implicit this argument, a non-member doesn't.

    Syntactically, you pass that implicit argument on the left of the . or -> operator like.so() or like->so(), instead of as a function argument so( like ).

    Likewise, when declaring a member function, you need to do so in the class of which it is a member:

    class Class {
    public:
        void a_public_member_function();
    };
    

    Non-member functions are instead declared outside any class (C++ calls this "at namespace scope").

    (Non-static) member functions can also be virtual, but non-member functions (and static member functions) cannot.

提交回复
热议问题