0

I hope the title is not confusing. I am trying to understand the following issue that arises from defining methods of a class virtual or not in C++. Imagine I have a base class A and a derived class B, such that

class A { 
public:
    void print() { cout << "A"; } 
}

class B : A { 
public:
    void print() { cout << "B"; } 
}

If I know execute the code below, the print command would print out "A".

A *a = new A();
B *b = new B();
((A *)b)->print(); // this prints "A"

However, if I declare the "print" methods in both classes as virtual, I would instead see a "B" printed in my screen. Why is this happening exactly?

4
  • 1
    Can you clarify the question "Why is this happening exactly?" Are you asking for a reference in the standard for where the language is specified to do this, or are you asking e.g. "what is a vtable, how is this actually working in a typical implementation"
    – Chris Beck
    Commented Sep 6, 2015 at 22:45
  • @ChrisBeck I know that the answer to the question is that if the methods are not virtual then the method invoked "depends on the type used at compile time", whereas if the methods are defined virtual then "the runtime type of the object" determines which definition is invoked. That is what I don't get exactly.
    – Adam
    Commented Sep 6, 2015 at 22:53
  • possible duplicate of Why do we need Virtual Methods in C++?
    – Chris Beck
    Commented Sep 6, 2015 at 22:57
  • What happens is that an old style cast is used to access a private base class; an implicit conversion would not work, not a static_cast, and reinterpret_cast might work in practice but give UB.
    – curiousguy
    Commented Sep 8, 2015 at 4:20

2 Answers 2

1

If a function is NOT virtual, the compiler will just use whatever type the expression gives. So when you cast a B object to an A object, it will call the A::print function.

If you use virtual, a table of function pointers [1] is built by the compiler, and when the function is to be called, the compiler generates code to call through that table, rather than just looking at the current type, which allows a base-type to call function in a derived class, and thus allowing polymorphic behaviour.

[1] technically, the specification doesn't tell us how to implement this, but this is how nearly all compilers work. If the compiler can produce the same behaviour using magic, it is allowed to do so - as long as the magic is reliable and reproducable.

1

A *b = new B(); //LHS is compile time = RHS is runtime(coz' object is created at runtime)
B.print();

if print is non virtual:
Compile time: A.print() is resolved to goto work (since it is a real function)
Runtime : A.print() is deployed/dispatched to work.

If print is a Virttual/ not real funtion:
Compile time: A.print() is bypassed and B.print is resolved to goto work runtime: B.print() is deployed/dispatched to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.