Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Default argument
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Default arguments in C++ == Consider the following function declaration: <syntaxhighlight lang="cpp"> int MyFunc(int a, int b, int c = 12); </syntaxhighlight> This function takes three arguments, of which the last one has a default of twelve. The programmer may call this function in two ways: <syntaxhighlight lang="cpp"> int result = MyFunc(1, 2, 3); result = MyFunc(1, 2); </syntaxhighlight> In the first case the value for the argument called ''c'' is specified explicitly. In the second case, the argument is omitted, and the default value of ''12'' will be used instead. For the function called, there is no means to know if the argument has been specified by the caller or if the default value was used. The above-mentioned method is especially useful when one wants to set default criteria so that the function can be called with or without parameters. Consider the following: <syntaxhighlight lang="cpp"> void PrintGreeting(std::ostream& stream = std::cout) { // This outputs a message to the given stream. stream << "hello world!"; } </syntaxhighlight> The function call: <syntaxhighlight lang="cpp"> PrintGreeting(); </syntaxhighlight> will by default print "[["Hello, World!" program|hello world]]!" to the [[standard output]] <code>std::cout</code> (typically the screen). On the other hand, any object of type <code>std::ostream</code> can now be passed to the same function and the function will print to the given stream instead of to the standard output. The example below sets the <code>std::ostream&</code> to <code>std::cerr</code>, and thus prints the output the standard error stream. <syntaxhighlight lang="cpp"> PrintGreeting(std::cerr); </syntaxhighlight> Because default arguments' values are "filled in" at the call site rather than in the body of the function being called, [[virtual function]]s take their default argument values from the static type of the pointer or reference through which the call is made, rather than from the dynamic type of the object supplying the virtual function's body. <syntaxhighlight lang="cpp"> struct Base { virtual std::pair<int, int> Foo(int x = 1) { return {x, 1}; } }; struct Derived : public Base { std::pair<int, int> Foo(int x = 2) override { return {x, 2}; } }; int main() { Derived d; Base& b = d; assert(d.Foo() == std::make_pair(2, 2)); assert(b.Foo() == std::make_pair(1, 2)); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)