Link to home
Start Free TrialLog in
Avatar of Ingo Foerster
Ingo Foerster

asked on

What means virtual

Hi,
can someone explain in easy words (for Dummies) what is the difference between a virtual function and a normal function?

vitrual void myfunc();
or
void myfunc();
Avatar of Russ Suter
Russ Suter

This is only important if you're using inheritance.

A virtual function is one that performs some kind of function in a base class but it can be overridden by a different version of the same function in a derived class.
class parent {
public:
  virtual void handle_event(int something) {
    // boring default code
  }
};

class child : public parent {
public:
  virtual void handle_event(int something) {
    // new exciting code
  }
};

int main() {
  parent *p = new child();
  p->handle_event(1);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> This is only important if you're using inheritance.
Agree.

>> The difference between a virtual and non-virtual function is the point at which the binding happens.
Agree, with some semantic clarification. I would recommend changing this sentence to:
One difference between a virtual and non-virtual function is the point at which the dynamic dispatching happens.

>> explain in easy words (for Dummies)
Since there are chapters on this subject, and since evilrix already gave you a number of links to chapters that touch on this subject, I have to agree that this is not an easy request. But, here goes..

In Russ Suter's example, even though the handle_event() method is virtual, I would expect that the compiler optimizer would be able to statically associate the child instance with the handle_event() method since the instance pointer and the method invocation is unambiguous. But, technically, evilrix is correct in that the language is accepting that Suter's code represents dynamic late dispatching. The optimizer saves a few cycles in Suter's code by not having to do that.
int main() {
  parent *p = new child();
  p->handle_event(1);
}

Open in new window

If the pointer, p, were passed into another function, foo(), especially a foo() in another compilable unit, then the compiler might not know whether the pointer is pointing to a base class object, parent, or one of its derived objects. So at the time of compilation, the compiler cannot statically call the desired method. In fact, each time foo() is called, the parent pointer could be pointing to a different class instance - either the parent, or one of its derived class instances.

BTW, as a rule of thumb, it is a good idea to always include a virtual destructor in a class whenever there are virtual methods. But that is another topic.
>> Agree, with some semantic clarification
Yup, I would agree with your agreement and semantic clarification. Thanks. It's actually amazing how hard it is to articulate these concepts in a "for dummies" kind of way. Thanks, phoffric. :)
Avatar of Ingo Foerster

ASKER

Gret, I have understood with first reading. Thank you