Hacker News new | ask | show | jobs
by millstone 3826 days ago
C++ its own insane behavior, where default arguments are determined at the call site instead of at runtime. This means your function can get another function's default arguments.

    #include <iostream>
    struct Base {
        virtual void foo(const char *name = "base") {
            std::cout << "Base impl with " << name << " param\n";
        }
    };
    
    struct Derived : public Base {
        virtual void foo(const char *name = "derived") override {
            std::cout << "Derived impl with " << name << " param\n";
        };
    };
    
    int main(void) {
        Base *b = new Derived();
        b->foo();
    }
prints "Derived impl with base param"

Fortunately Python avoided that bit of silliness.

1 comments

This behavior is pretty reasonable to me.