C++ 在C++中,方法和函数有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8596461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In C++, what is the difference between a method and a function
提问by Wes
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.
就 C++ 而言,方法和函数之间有什么区别。
Is it that a method returns nothing and just preforms operations on its class; while a function has a return value?
是不是一个方法什么都不返回,只是在它的类上执行操作;而函数有返回值?
回答by Oliver Charlesworth
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.
就 C++ 标准而言,没有“方法”这样的东西。该术语在其他 OO 语言(例如 Java)中用于指代类的成员函数。
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).
在通常的用法中,您会发现大多数人或多或少会交替使用“方法”和“函数”,尽管有些人会将“方法”的使用限制在成员函数(而不是“自由函数”)类的成员)。
回答by Brian Neal
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.
对不起,但这是我最讨厌的事情之一。方法只是一个通用的 OO 类型术语。C++ 中不存在方法。如果您打开 C++ 标准,您将找不到任何提及“方法”的内容。C++ 具有各种风格的函数。
回答by Seth Carnegie
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).
方法是类的成员函数,但在 C++ 中,它们更常被称为成员函数而不是方法(一些来自其他语言(如 Java)的程序员将它们称为方法)。
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
回答by Captain Giraffe
The term "Method" is not used in c++, but rather member function.
C++ 中不使用术语“方法”,而是使用成员函数。
If you are thinking about the difference between a procedureand 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.)
如果您正在考虑过程和函数之间的区别,那么 c++ 中的区别是没有的。Pascal 几乎是做出这种区分的最后一种语言。(ADA 是后来构建的,并使用了“程序”一词,感谢布赖恩·尼尔(Brian Neal)。)
Any function, member or not, declared as void, would be a Procedure in the old vocabulary.
任何声明为 void 的函数,无论是否为成员,都将是旧词汇表中的过程。
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
- 是一个类的成员
- 可以是私人的
- 可以保护
- 可以公开
- 可以是虚拟的
- 可以是纯虚拟的
回答by Christian Nowak
Even a method can have a return value.
甚至一个方法也可以有一个返回值。
A method is a function of a class. For example class "car" has a method "accelerate".
方法是类的函数。例如类“汽车”有一个方法“加速”。