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

wanmeiluck123 2013-06-13 11:36:15
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, ...)这种不定形参写法与其相比又如何?
...全文
318 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hugett 2013-06-13
  • 打赏
  • 举报
回复
有没有void完全木有区别。。
turing-complete 2013-06-13
  • 打赏
  • 举报
回复
C++中 1. 没有区别 2. 没有重载,你写了之后,编译器不会再自动生成这个版本的构造函数 3. 没有区别
逸萌 2013-06-13
  • 打赏
  • 举报
回复
C/C++中没区别
lin5161678 2013-06-13
  • 打赏
  • 举报
回复
没区别~~ 注 C++中没区别
赵4老师 2013-06-13
  • 打赏
  • 举报
回复
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++ }
www_adintr_com 2013-06-13
  • 打赏
  • 举报
回复
在 C++ 里, void foobar() 与 void foobar(void) 是没有区别的, 构造函数和 main 里也一样. 在 C 语言里, void foobar(); 的声明表示不知道这个函数有多少个参数, 所以调用的时候可以传任意的参数, 但是和定义的时候不匹配的话是会出错的. void foobar(void);表示没有参数. 如果出现在定义函数的哪里, 他们都表示没有参数了. foobar(int argc, ...) 是指明了函数可以接受不定参数, void foobar(); 只是不知道它接受什么参数, 编译器不做检查, 并不代表函数真的可以接受任意的参数.

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧