let's do another exersise :
> #include <iostream>
> using namespace std;
>
> class Base {
> public:
> virtual int f() const { return 1; }
> };
>
> class Derived : public Base {
> public:
> int f() const { return 2; }
void foo()
{
this->~Derived();
new (this)Base;
f(); // is here late or early binding ???
// according your words, here must by early binding. right? look at the results
}
> };
>
> int main() {
> Derived d;
> Base* b1 = &d;
> Base& b2 = d;
> Base b3;
> // Late binding for both:
> cout << "b1->f() = " << b1->f()
> << endl;
> cout << "b2.f() = " << b2.f() << endl;
> // Early binding (probably):
^^^^^^^^^^^^^ good words. you can only suppose, but I gues you are not sure.
> cout << "b3.f() = " << b3.f() << endl;
> } ///:~
---
> In b1–>f( ) and b2.f( ) addresses are used, which means > the information is incomplete: b1 and b2 can represent the > address of a Base or something derived from Base, so the > virtual mechanism must be used. When calling b3.f( ) > there’s no ambiguity. The compiler knows the exact type and > that it’s an object, so it can’t possibly be an object > derived from Base — it’s exactly a Base. Thus early binding > is probably used. However, if the compiler doesn’t want to > work so hard, it can still use late binding and the same > behavior will occur. Very logic, but not completed. as i said we can only suppose. looking at the results you are right, but the question was is it correct ?
> > P.S. tema zakryta. З.Ы. давно пора.
|