*
:
>
> Can someone tell me how virtual functions works.
The nearest C++ textbook.
You can try
#include <iostream>
#include <ostream>
void say( char const s[] ) { std::cout << s << std::endl; }
struct Base
{
virtual void foo() const { say( "Base::foo" ); }
};
struct Derived: Base
{
virtual void foo() const { say( "Derived::foo" ); }
};
int main()
{
Base const& baseRef = Derived();
baseRef.foo();
}
> Like how memory is getting allocated to virtual function.
It isn't.
> And how to call base class function through derived class pointer.
p->Base::foo();
> And why virtual function can be access only through pointer.
There's no such restriction.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?