【问题标题】:Function Overloading with void in argument bracket of main function [duplicate]在主函数的参数括号中使用void重载[重复]
【发布时间】:2020-03-06 07:51:47
【问题描述】:

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

#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; 

}

【问题讨论】:

    标签: c++


    【解决方案1】:

    (void) 是 main 期望其调用者提供的参数列表。在大多数情况下,它的调用者是操作系统(或者,严格来说,调用程序中 main 函数的启动代码)。在 C 中,一个 void 参数列表明确指出该函数不希望从其调用者接收任何参数。在 C++ 中,参数列表 (void) 与空参数列表 () 具有相同的含义。 Find More Here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-04
      • 2016-10-09
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多