C++ 如果 void() 没有返回值,我们为什么要使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11560068/
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
If void() does not return a value, why do we use it?
提问by Mustafa Muhammad Yousif
void f()
means that f
returns nothing. If void
returns nothing, then why we use it? What is the main purpose of void
?
void f()
意味着f
什么都不返回。如果void
什么都不返回,那我们为什么要使用它?的主要目的是void
什么?
回答by Matteo Italia
When C was invented the convention was that, if you didn't specify the return type, the compiler automatically inferred that you wanted to return an int
(and the same holds for parameters).
当 C 被发明时,约定是,如果你没有指定返回类型,编译器会自动推断你想要返回一个int
(对于参数也是如此)。
But often you write functions that do stuff and don't need to return anything (think e.g. about a function that just prints something on the screen); for this reason, it was decided that, to specify that you don't want to return anything at all, you have to use the void
keyword as "return type".
但通常你编写的函数可以做一些事情并且不需要返回任何东西(例如考虑一个只在屏幕上打印一些东西的函数);出于这个原因,决定指定您根本不想返回任何内容,您必须使用void
关键字作为“返回类型”。
Keep in mind that void
serves also other purposes; in particular:
请记住,这void
也有其他用途;特别是:
if you specify it as the list of parameters to a functions, it means that the function takes no parameters; this was needed in C, because a function declaration without parameters meant to the compiler that the parameter list was simply left unspecified. In C++ this is no longer needed, since an empty parameters list means that no parameter is allowed for the function;
void
also has an important role in pointers;void *
(and its variations) means "pointer to something left unspecified". This is useful if you have to write functions that must store/pass pointers around without actually using them (only at the end, to actually use the pointer, a cast to the appropriate type is needed).also, a cast to
(void)
is often used to mark a value as deliberately unused, suppressing compiler warnings.int somefunction(int a, int b, int c) { (void)c; // c is reserved for future usage, kill the "unused parameter" warning return a+b; }
如果将其指定为函数的参数列表,则表示该函数不带参数;这在 C 中是需要的,因为没有参数的函数声明意味着编译器只是未指定参数列表。在 C++ 中不再需要它,因为空参数列表意味着该函数不允许使用参数;
void
在指针中也有重要作用;void *
(及其变体)的意思是“指向未指定内容的指针”。如果您必须编写必须存储/传递指针而不实际使用它们的函数(仅在最后,要实际使用指针,需要转换为适当的类型),这将非常有用。此外,强制转换
(void)
通常用于将值标记为故意未使用,从而抑制编译器警告。int somefunction(int a, int b, int c) { (void)c; // c is reserved for future usage, kill the "unused parameter" warning return a+b; }
回答by Nawaz
void() means return nothing.
void() 意味着什么都不返回。
void
doesn't mean nothing. void
is a typeto representnothing. That is a subtle difference : the representationis still required, even though it represents nothing.
void
不代表什么。void
是一种类型,以代表什么。这是一个微妙的区别:表示仍然是必需的,即使它什么都不表示。
This typeis used as function's return typewhich returns nothing. This is also used to represent generic data, when it is used as void*
. So it sounds amusing that while void
represents nothing, void*
represents everything!
此类型用作函数的返回类型,它不返回任何内容。当用作void*
. 所以这听起来很有趣,虽然void
不代表什么,但void*
代表一切!
回答by dasblinkenlight
This question has to do with the history of the language: C++ borrowed from C, and C used to implicitly type everything untyped as int
(as it turned out, it was a horrible idea). This included functions that were intended as procedures (recall that the difference between functions and procedures is that function invocations are expressions, while procedure invocations are statements). If I recall it correctly from reading the early C books, programmers used to patch this shortcoming with a #define
:
这个问题与语言的历史有关:C++ 是从 C 借来的,而 C 过去常常隐式地将所有未类型化的类型都输入为int
(事实证明,这是一个可怕的想法)。这包括旨在作为过程的函数(回想一下,函数和过程之间的区别在于函数调用是表达式,而过程调用是语句)。如果我在阅读早期的 C 书籍时没有记错的话,程序员过去常常用以下代码修补这个缺点#define
:
#define void int
This convention has later been adopted in the C standard, and the void
keyword has been introduced to denote functions that are intended as procedures. This was very helpful, because the compiler could now check if your code is using a return value from a function that wasn't intended to return anything, and to warn you about functions that should return but let the control run off the end instead.
此约定后来被 C 标准采用,并且void
引入了关键字来表示旨在作为过程的函数。这非常有用,因为编译器现在可以检查您的代码是否使用了一个不打算返回任何内容的函数的返回值,并警告您应该返回但让控件运行到最后的函数。
回答by waldrumpus
In imperative programming languages such as C, C++, Java, etc., functions and methods of type void
are used for their side effects. They do not produce a meaningful value to return, but they influence the program state in one of many possible ways. E.g., the exit
function in C returns no value, but it has the side effect of aborting the application. Another example, a C++ class may have a void method that changes the value of its instance variables.
在 C、C++、Java 等命令式编程语言中,类型的函数和方法void
用于它们的副作用。它们不会产生有意义的返回值,但它们会以多种可能的方式之一影响程序状态。例如,exit
C 中的函数不返回任何值,但它具有中止应用程序的副作用。另一个例子,一个 C++ 类可能有一个 void 方法来改变它的实例变量的值。
回答by Rango
Because sometimes you dont need a return value. That's why we use it.
因为有时您不需要返回值。这就是我们使用它的原因。
回答by Some programmer dude
If you didn't have void
, how would you tell the compiler that a function doesn't return a value?
如果你没有void
,你怎么告诉编译器一个函数没有返回值?
回答by Vins
Cause consider some situations where you may have to do some calculation on global variables and put results in global variable or you want to print something depending on arguments , etc.. In these situations you can use the method which dont return value.. i.e.. void
原因考虑某些情况,您可能必须对全局变量进行一些计算并将结果放入全局变量中,或者您想根据参数等打印某些内容。在这些情况下,您可以使用不返回值的方法。即。空白
回答by Mateusz N
Here's an example function:
这是一个示例函数:
struct SVeryBigStruct
{
// a lot of data here
};
SVeryBigStruct foo()
{
SVeryBigStruct bar;
// calculate something here
return bar;
}
And now here's another function:
现在这是另一个功能:
void foo2(SVeryBigStruct& bar) // or SVeryBigStruct* pBar
{
bar.member1 = ...
bar.member2 = ...
}
The second function is faster, it doesn't have to copy whole struct.
第二个函数更快,它不必复制整个结构。
回答by Zain
Functions are not required to return a value. To tell the compiler that a function does not return a value, a return type of void is used.
函数不需要返回值。为了告诉编译器一个函数没有返回值,使用了 void 返回类型。
回答by huseyin tugrul buyukisik
probably to tell the compiler " you dont need to push and pop all cpu-registers!"
可能是告诉编译器“您不需要推送和弹出所有 cpu 寄存器!”