   My C++ compiler went Java?
  Hello all!I was playing with some virtual functions, trying to get a better grasp of them.After many tests I tried to do the following:struct B {   void f() const {std::cout &lt;&lt; "B::f ";}   virtual void fv() const {std::cout &lt;&lt; "Bv::f ";}};struct C : public B{   void fv() const {std:: cout &lt;&lt; "Cv::f";}};int main(){    B b;    C c;    b.f();    b.fv();    c.f();    c.fv();}</code>the result was:B::f Bv::f B::f Cv::fWhy is that so? wasn't the 3rd item supposed to be accessible &#111;nly to B's members? How C inherited a function that wasn't marked as virtual in the Base class?Or am I get confused by the use of virtual members? Because when I write a member function in a derived class with the same signature as that of the Base class, the function actually called is the derived's &#111;ne. What is happening internally. It won't be using a virtual table, so, probably there is repetition in memory... is that correct?cheers!
  You've got it wrong. C inherits all members of B, even non-virtual ones. Marking a function as virtual allows subclasses to extend or replace the function's behavior, but is not a prerequisite for inheriting it.   ;Quote:Original post by WindryderYou've got it wrong. C inherits all members of B, even non-virtual ones. Marking a function as virtual allows subclasses to extend or replace the function's behavior, but is not a prerequisite for inheriting it.Marking a method as virtual guarantees that the correct overridden (if any) version is called even if you are invoking it through a reference/pointer of a base class.Suppose that class BaseClass defines a method AMethod() and that DerivedClass overrides that method. Then:DerivedClass* c = new DerivedClass();c->AMethod(); //The correct overridden method is calledBaseClass* c = new DerivedClass();c->AMethod(); //The correct version is called only if it is marked as virtualIn java all methods are virtual by default.   