In C++, what is the difference between a method and a function

Possible Duplicate:
What is the difference between a method and a function

I'm trying to get my terminology correct.

What is the difference between a method and a function, in regards to C++ specifically.

Is it that a method returns nothing and just preforms operations on its class; while a function has a return value?


As far as the C++ standard is concerned, there is no such thing as a "method". This terminology is used in other OO languages (eg Java) to refer to member functions of a class.

In common usage, you'll find that most people will use "method" and "function" more or less interchangeably, although some people will restrict use of "method" to member functions (as opposed to "free functions" which aren't members of a class).


Sorry, but this is one of my pet peeves. Method is just a generic OO-type term. Methods do not exist in C++. If you open the C++ standard, you won't find any mention of "methods". C++ has functions, of various flavors.


A method is a member function of a class, but in C++ they are more commonly called member functions than methods (some programmers coming from other languages like Java call them methods).

A function is usually meant to mean a free-function, which is not the member of a class.

So while a member function is a function, a function is not necessarily a member function.

Example:

void blah() { } // function

class A {
    void blah() { } // member function (what would be a "method" in other languages)
};

blah(); // free functions (non-member functions) can be called like this

A ainst;
ainst.blah(); // member functions require an instance to invoke them on
链接地址: http://www.djcxy.com/p/17308.html

上一篇: Java的方法与功能

下一篇: 在C ++中,方法和函数之间有什么区别