In C++, what is the difference between a method and a function [duplicate]
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?
Solution 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).
Solution 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.
Solution 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