Is there any elegant way to acheive following:
class Base {
public:
Base() {}
virtual ~Base() {}
virtual void Method() { cout << "Base::Meth od called"; return;
}
};
class Derived : public Base {
public:
Derived() {}
~Derived()
void Method() { cout << "Derived::Metho d called"; return; }
};
int main() {
Derived deriveObj;
Base * basePtr = 0;
basePtr = <some kind of cast????> &deriveObj;
basePtr->Method();
}
In the above code, the call "basePtr->Method" should print
"Base::Meth od called" and not "Derived::Metho d called".
Is there any way to assign address of "deriveObj" to "basePtr" so that
the virtual mechanism is bypassed and call to member function "Method"
actually calls the member function from the "class Base" and not from
"class Derived".
Thanks in advance,
Regards,
Abhijit.
class Base {
public:
Base() {}
virtual ~Base() {}
virtual void Method() { cout << "Base::Meth od called"; return;
}
};
class Derived : public Base {
public:
Derived() {}
~Derived()
void Method() { cout << "Derived::Metho d called"; return; }
};
int main() {
Derived deriveObj;
Base * basePtr = 0;
basePtr = <some kind of cast????> &deriveObj;
basePtr->Method();
}
In the above code, the call "basePtr->Method" should print
"Base::Meth od called" and not "Derived::Metho d called".
Is there any way to assign address of "deriveObj" to "basePtr" so that
the virtual mechanism is bypassed and call to member function "Method"
actually calls the member function from the "class Base" and not from
"class Derived".
Thanks in advance,
Regards,
Abhijit.
Comment