I was reading about Virtual Functions from the book "The C++ Programming Langauge" by Bjarne Stroustrup, and encountered the following code snippet:-
class A {
//...
protected:
int someOtherField;
//...
public:
virtual void print() const;
//...
};
class B : public A {
//...
public:
void print() const;
//...
};
void B::print() const {
A::print();
cout<<A::someOtherField;
//...
}
It is written in the book that
"Calling a function using the scope resolution operator(::) as done in B::print() ensures that virtual mechanism is not used. Otherwise, B::print() would suffer infinite recursion."
I do not understand why this is the case, since, a call to the base class function correctly and explicitly tells that we are calling A::print() and not anything else. Why this may lead to infinite recursion?
Edit - I misplaced the keyword "virtual", I am extremely sorry for that, but still exploring this question also, What would happen if the following code was there?
class A {
//...
void print() const;
//...
}
class B : public A {
//...
virtual void print() const;
//...
}
If qualified call A::print()
did not disable virtual dispatch, the usage like presented in B::print()
would be infinite recursion and it would be pretty much impossible to call function from base class.
See the imaginary code execution if qualified call did not disable virtual dispatch:
A* a = new B;
a->print()
is called, virtual dispatch determines that B::print()
should be calledB::print()
calls A::print()
, virtual dispatch determines that B::print()
should be calledNow, an execution sequence when qualified call disables virtual dispatch:
A* a = new B;
a->print()
is called, virtual dispatch determines that B::print()
should be calledB::print()
calls A::print()
, exactly this function is calledA::print()
does its things and finishesB::print()
continues its execution.