原文地址:https://stackoverflow.com/questions/21045615/what-does-voidvar-actually-do
弄清楚C++中的一些细节,确实很有意思,值得回味。
在C++中会看到一些函数参数没有用到,但是又不能去掉(例如继承下来的虚函数,暂时不想实现),这时候就可以在实现中(void)var,不然编译器会告警。
例如:
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
return (0);
}
It's just a way of creating a 'harmless' reference to the variable. The compiler doesn't complain about an unused variable, because you did reference the value, and it doesn't complain that you didn't do anything with the value of the expression var
because you explicitly cast it to void (nothing), indicating that you didn't care about the value.