Good day. I stumbled upon a function in which it is not clear what is written in the parameters:

virtual void funcC (double = 0.0)

If there was a variable name, it would be clear what the parameter is or if this thing would not compile, it would be a typo) So no, everything compiles and works. What is this, what is its meaning: double = 0.0 ?

    1 answer 1

    Default value.

    If you call it as Obj.funcC() , it will be interpreted as Obj.funcC(0.0) .

    In short, read what the default values ​​of the function arguments are - for example, here .

    There is no name for the variable - because it may simply be reserved or not needed. For example, in the successor it can be necessary. And in the base class it is not needed.

     struct Base { virtual void out(double = 0.0) { cout << "Base::out\n"; } }; struct Derived: public Base { virtual void out(double x) { cout << "Drived::out " << x << "\n"; } }; int main(int argc, const char * argv[]) { Base * b = new Derived; b->out(); // Тут вызовется out() из Derived со значением 0.0 b->out(3); // Тут - со значением 3.0 } 

    The name of the argument in Base is not needed - it is not used, and is needed only to provide a signature.

    If you enter a name, the compiler will fairly note that the name is, but is not used. Here - he will keep silent.

    • I know what a default value is. You saw that there was no variable name in the parameters. Therefore, it is not clear to what this default value is applied. And what to do if the value is assigned incomprehensibly where? - Alerr
    • Well, I thought you were aware of unnamed arguments. Painted more. - Harry
    • Thank you!) Another question, not the topic, about virtuality. Why write virtual in the successor? It works without it. - Alerr
    • 2
      If I have in the code - for two reasons: copy-paste :) + habit - when I started with C ++, the word override was not there, and this virtual reminded me that the function was virtual inherited. How documentation :) - Harry