读书人

C++参数中void形参的有无有关问题

发布时间: 2013-07-01 12:33:04 作者: rapoo

C++参数中void形参的有无问题
1.普通全局函数void foobar();与void foobar(void);有无区别
2.构造函数foobar::foobar()与foobar::foobar(void)有无区别,如果我写了foobar::foobar(void),这是不是重载了默认构造函数
3.int main()与int main(void)有无区别

好像听到别人说形参没有void可以代表有N多个参数,不知道对于C++正不正确
如果正确的话,那foobar(int argc, ...)这种不定形参写法与其相比又如何?
[解决办法]
在 C++ 里, void foobar() 与 void foobar(void) 是没有区别的, 构造函数和 main 里也一样.

在 C 语言里, void foobar(); 的声明表示不知道这个函数有多少个参数, 所以调用的时候可以传任意的参数, 但是和定义的时候不匹配的话是会出错的. void foobar(void);表示没有参数. 如果出现在定义函数的哪里, 他们都表示没有参数了.
foobar(int argc, ...) 是指明了函数可以接受不定参数, void foobar(); 只是不知道它接受什么参数, 编译器不做检查, 并不代表函数真的可以接受任意的参数.
[解决办法]
void
void declarator

When used as a function return type, the void keyword specifies that the function does not return a value. When used for a function's parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is "universal."

If a pointer's type is void *, the pointer can point to any variable that is not declared with the const or volatile keyword. A void pointer cannot be dereferenced unless it is cast to another type. A void pointer can be converted into any other type of data pointer.

A void pointer can point to a function, but not to a class member in C++.

You cannot declare a variable of type void.

Example

// Examples of the void keyword
void vobject; // Error
void *pv; // Okay
int *pint; int i;
void main() // main has no return value
{
pv = &i;
pint = (int *)pv; // Cast optional in C
// required in C++
}

[解决办法]
没区别~~
注 C++中没区别
[解决办法]
C/C++中没区别
[解决办法]
C++中

1. 没有区别
2. 没有重载,你写了之后,编译器不会再自动生成这个版本的构造函数


3. 没有区别
[解决办法]
有没有void完全木有区别。。

读书人网 >C++

热点推荐