简体   繁体   中英

Function argument already initialized in function declaration C++

So here's my question in the function declaration there is an argument and it is already initialized to a certain value. What are the procedures to call this function and use that default value, or is it just a way to document code, telling other programmers what you expect them to use as a value for the parameter? Thank you.

enum File
{
    XML = 0,
    PDF = 1,
};

Char *SetSection(const File = XML);

If I understand your question correctly, simply calling SetSection with no parameters will do.

SetSection();

The above call gets translated (for lack of a better term) to:

SetSection(XML);

这意味着可以在没有参数的情况下调用该函数,在这种情况下将使用默认值XML。

In this case, File is a default argument . Calling SetSection() without arguments will set the File argument to the default value specified in the declaration.

If you call

SetSection();

It will call SetSection(XML) instead. This is why the optional parameters have to be at the end of all parameters. If you don't provide enough parameters, it will use the default.

XML is the standard parameter. You can call it with SetSection(); (But SetSection(XML) or SetSection(PDF) are valid, too).

What you are seeing in the declaration is a default argument value.

If you call SetSection(); it is the same as calling SetSection(XML);

You can also call SetSelection(PDF); or use any other valid parameter to override the default.

You may also be seeing the result of an incremental development which started with the function having no parameter and calls to the function scattered throughout the code. Then the alternative file type of PDF was introduced, but the prototype was changed to have a default value, which meant not having to change the existing call site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM