首页 文章

c void只有参数类型

提问于
浏览
0

我正在阅读一些教程并制作代码,但遇到了一个简单奇怪的函数方法,一个void函数与 just parameter type 不是实际参数,如:"void foo(int)" .

我知道,如果我创建一个像“ void foo(void) ”的代码,这意味着它绝对不允许我传递参数但不知道如果参数有 just type 'int.' 这甚至不是函数声明,而是函数 definition .

首先,我认为这可能意味着我可以传递任何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)
}

3 回答

  • 3

    参数的名称实际上是可选的 . 如果参数不在函数体中使用,则声明 void display(int) 完全正常 .

    为了调用此方法,只需将 int 作为参数传递 .

  • 3

    只需 name if you use it . 由于 parameter 未在 method 中使用,因此不提供名称不会造成任何问题 .

  • 2

    你可以简单地传递一个整数 . 它不使用它,但在这种情况下无关紧要 .

    sa.display(0);
    

    如果您打算在子类中重载此函数,并且实际将使用此整数,则此方法在实际情况下可能很有用 .

相关问题