主函数自变量括号中的函数重载为空

时间:2019-11-10 10:06:36

标签: c++

在这段代码中,我正在重载该函数。但是有人可以告诉我为什么main函数的参数括号中没有空白。我试图从主要功能代码的括号中删除空白仍然有效。有什么想法吗?

#include <iostream.h> 

class printData 

{ 

public: 

void print(int i) 

{

cout << "Printing int: " << i << endl; 

} 

void print(double f) 

{ 

cout << "Printing float: " << f << endl; 

} 

void print(char* c) 

{ 

cout << "Printing character: " << c << endl; 

} 

}; 

int main(Void) 

{ 

printData pd; 

pd.print(5);        // Call print to print integer

pd.print(500.263);      // Call print to print float 

pd.print("Hello C++");  // Call print to print character 

return 0; 

}

1 个答案:

答案 0 :(得分:0)

(void)是main期望其调用者的参数列表。在大多数情况下,其调用者是操作系统(或严格来说,是调用程序中主要功能的启动代码)。在C中,无效参数列表明确声明该函数不希望从其调用方接收任何参数。在C ++中,参数列表(void)与空参数列表()的含义相同。 Find More Here