Advertisement

virtual keyword...

Started by March 03, 2002 04:16 AM
2 comments, last by elis-cool 22 years, 6 months ago
What is the point of it? because when you have a function in a base class then use the same function name in a derived class it overides it anyway, is it there just so when you are looking at the base class code you can tell that it will possibly be overidden in a derived class by a virtual keyword in front, but it effectivly does nothing?
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Here is some example code and what it does (unless I'm mistaken).

    class A{public:   void foo() { cout << "A::foo" << endl; }   virtual void bar() { cout << "A::bar" << endl };      void testA() { foo(); bar() };}class B : public A{public:   void foo() { cout << "B::foo" << endl; }   virtual void bar() { cout << "B::bar" << endl };      void testB() { foo(); bar() };}... executionA* pA = new A;B* pB = new B;pA->testA(); // prints A::foo, A::barpB->testA(); // prints A::foo, B::barpB->testB(); // prints B::foo, B::barA* pC = new B;pC->testA(); // prints A::foo, B::barpC->testB(); // ERROR, no testB for an A*  


A non-virtual function relies on the type of the pointer variable to determine the type of the object, and thus what function to call (here A::foo vs B::foo).

A virtual function uses information which identifies the real type of the pointed object. Even when you pass a B as an A, the right version of bar is called.

Edited by - Fruny on March 3, 2002 5:34:52 AM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
virtual are used to achieve run-time polymorphism
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
quote: Original post by elis-cool
What is the point of it? because when you have a function in a base class then use the same function name in a derived class it overides it anyway, is it there just so when you are looking at the base class code you can tell that it will possibly be overidden in a derived class by a virtual keyword in front, but it effectivly does nothing?


There is a rather large difference between using virtual and not. The virtual keyword is required to provide polymorphic behaviour. Redefining a non-virtual in a derived class would actually result in *hiding* the version from the base class and introducing a different function of the same name. In the case of deleting a derived object through a pointer to it''s base, failing to make the base destructor virtual will cause undefined behaviour.

To illustrate the difference, try stepping through the following code in your debugger and see which version of f() actually gets called. Try it with f() as both virtual and non-virtual.

  struct base{	//virtual 		void f() {}	virtual ~base() {}};struct derived : public base{	void f() {}};int main(){	base *obj = new derived;	obj->f();	delete obj;}  


--
You see some pale bulbous eyes staring at you.

This topic is closed to new replies.

Advertisement