📜 ⬆️ ⬇️

Unexpected deceitfulness of default parameters or my language is my enemy

, , , .

. , ? « », .

#include <iostream>
using namespace std;

class A {
public:
    virtual void Foo (int n = 10) {
        cout << "A::Foo, n = " << n << endl;
    }
};

class B : public A {
public:
    virtual void Foo (int n = 20) {
        cout << "B::Foo, n = " << n << endl;
    }
};

int main() {
    A * pa = new B ();
    pa->Foo ();

    return 0;
}


! , ? !

B::Foo, n = 10

, B::Foo, n = 20, . , ?! , , B. … …

, ! «» , .

, , . A::Foo, n = 10, , ++. , - , .

, ?

, , . A * , A::Foo.

— §8.3.6, 10 working paper ISO/IEC 14882: Programming Language C++. ( working paper . 1998 .)

, , ? , . , .

? , . , , . , /W4 -Wextra . ( Visual C++ 2008 SP1 gcc 3.4.4.)

, — . — , , - , . ? .

. . .

. C++ . , ++ . : C++ , , .

P.S. , no_smoking . !

')

Source: https://habr.com/ru/post/53576/


All Articles