0

I basically copied this example from Item 21. Overriding Virtual Functions in Herb Sutter's book Exceptional C++.

#include <iostream>

class Base {
public:
    virtual void f(int i = 10) { std::cout << i << '\n'; }
};

class Derived : public Base {
public:
    void f(int i = 20) { std::cout << i << '\n'; }
};

int main()
{
    Base* p = new Derived;
    p->f();
}

Surprisingly (for me at least) the code prints 10 (not 20) and the author explains this with the following words in page 122: The thing to remember is that, like overloads, default parameters are taken from the static type (here Base) of the object, hence the default value of 10 is taken. However, the function happens to be virtual, so the function actually called is based on the dynamic type (here Derived) of the object.

Is there any quote in the C++11 Standard supporting this?

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
Wake up Brazil
  • 3,421
  • 12
  • 19

2 Answers2

2

8.3.6/10:

A virtual function call (10.3) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • 1
    You normally want to differentiate between the paragraph number and the other section numbering, such as `8.3.6/10` or `8.3.6p10` or else use the tag, like `[dcl.fct.default]/10`. Although it's not in this specific case, there are cases where there are both a `x.y.z` and a `x.y/z` that are separate from each other. – Jerry Coffin May 18 '14 at 23:54
  • @vsoftco I'll accept your answer. I received it a few seconds before Jerry Coffin's. Thanks to both. – Wake up Brazil May 18 '14 at 23:57
  • @WakeupBrazil: If you hover your mouse over the `answered x minutes ago` you'll see the time at which each was actually posted (showing, that in this case mine was actually posted about 30 seconds before his). – Jerry Coffin May 18 '14 at 23:58
  • @vsoftco Jerry is right. I'll have to give him the credit for the answer. I noticed your comment about the possible dup first, but he answered the question a little bit faster than you. – Wake up Brazil May 19 '14 at 00:06
1

§8.3.6/10 (aka [dcl.fct.default]/10):

A virtual function call (10.3) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides. [emphasis added]

This specific quote is from N3337, but to the best of my recollection, this part of C++ has remained essentially constant over the years, so I would expect any more than the most minor wording changes between different revisions of the standard.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Hi Jerry, sorry for the race condition :) My answer was first automatically converted into a comment, and you posted it while I was editing :) – vsoftco May 19 '14 at 00:01
  • +1 Because this is a much better answer, plus you answered it first. – 101010 May 19 '14 at 00:01