问题
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?
回答1:
As far as the C++ standard is concerned, there is no such thing as a "method". This terminology is used in other OO languages (e.g. 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).
回答2:
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.
回答3:
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
回答4:
The term "Method" is not used in c++, but rather member function.
If you are thinking about the difference between a procedure and a function then the difference in c++ is none. Pascal was pretty much the last language to make that distinction. (ADA was constructed later and used the term Procedure, thanks Brian Neal.)
Any function, member or not, declared as void, would be a Procedure in the old vocabulary.
A member function is a complex beast, a function is a simple function.
A member function
- is a member of a class
- can be private
- can be protected
- can be public
- can be virtual
- can be pure virtual
回答5:
Even a method can have a return value.
A method is a function of a class. For example class "car" has a method "accelerate".
来源:https://stackoverflow.com/questions/8596461/in-c-what-is-the-difference-between-a-method-and-a-function