【发布时间】:2014-06-29 17:24:08
【问题描述】:
我正在阅读一些教程并编写代码,但遇到了一个简单的奇怪函数方法,即具有 仅参数类型而不是实际参数的 void 函数,例如:“void foo(int)”。
我知道如果我编写像“void foo(void)”这样的代码,这意味着它绝对不允许我传递参数,但不知道如果参数有 意味着什么只需输入'int'。它甚至不是一个函数声明,而是一个函数定义。
首先,我认为这可能意味着我可以传递任何 int 变量,但事实并非如此。如果有人知道这个,你能教我这是什么吗?感谢您的阅读和您的时间。代码如下:
struct SA{
void display() { cout << "struct sA without int" << endl; }
void display(int) { cout << "Struct sA" << endl; } // this is it. I have no idea what means putting just type int. I can't even use this function
};
int main(){
SA sa;
sa.display(); // I had no problem to call display() but have no idea how to use diplay(int)
}
【问题讨论】:
-
void display(int param)也许?? -
感谢您的评论πάντα ῥεῖ!不知何故,它只是类型而不是变量,但现在我认为避免这种方式并使用你提到的参数绝对是好的。感谢您的快速回复!