C ++ Late Binding on function overloads

Assuming I have these two classes:

class Hello {
    //members
public:
    virtual int doit() {
        return 3;
    }

};

class Wow : public Hello {
    //members
public:
    virtual int doit() {
        return 2;
    }
};

int main(int argc, const char *argv[]) {

    Hello *c = new Wow();

    return c->doit();
}

As we all know, this code will be handled by late binding in C ++ implemented in LLVM IR Clang as follows:

 ; ...
  %7 = bitcast %class.Wow* %5 to %class.Hello*
  store %class.Hello* %7, %class.Hello** %c, align 8
  %8 = load %class.Hello*, %class.Hello** %c, align 8
  %9 = bitcast %class.Hello* %8 to i32 (%class.Hello*)***
  %10 = load i32 (%class.Hello*)**, i32 (%class.Hello*)*** %9, align 8
  %11 = getelementptr inbounds i32 (%class.Hello*)*, i32 (%class.Hello*)** %10, i64 0
  %12 = load i32 (%class.Hello*)*, i32 (%class.Hello*)** %11, align 8
  %13 = call i32 %12(%class.Hello* %8)
 ; ...

My question is: what if I want to create a function called check, for example, in a different namespace, for example:

namespace somewhereelse {
   void check(Hello *c) {
    // Do something
   }
   void check(Wow *c) {
    // Do something else
   }
}

Could some last binding be applied to various function overloads?

+4
source share
2 answers

No, dynamic dispatch for non-member functions is not currently part of C ++.

, , . ++ ( : multimethods ), .

+4

.

void (*checkPtr)(Hello*);
...
void check(Hello *c) {
    // Do something
   }
...
checkPtr = ✓

, .

-1

All Articles