What is the meaning of having void in the constructor definition?

后端 未结 4 642
我寻月下人不归
我寻月下人不归 2021-02-13 02:28

Given the following code:

#pragma once

class B
{
public:

    B(void)
    {
    }

    ~B(void)
    {
    }
};

I know I can also write this:

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-13 02:59

    A long time ago you did something like this in C (my pre-ISO C is rusty :) ):

    void foo(a, b)
       int a, 
       int b
    {
    }
    

    while C++ was being created the name mangling required the types of the arguments, so for C++ it was changed to:

    void foo(int a, int b)
    {
    }
    

    and this change was brought forward to C.

    At this point, I believe to avoid breaking existing C code this:

    void foo() 
    

    and this:

    void foo(void)
    

    meant two very different things, () means do not check for the argument number or type, and (void) means takes no arguments. For C++ () meaning not to check anything was not going to work so () and (void) mean the same thing in C++.

    So, for C++ () and (void) were always the same thing.

    At least that is how I remember it... :-)

提交回复
热议问题