Click here to Skip to main content
16,018,460 members
Please Sign up or sign in to vote.
1.26/5 (5 votes)
See more:
What are the advantages and disadvantages of virtual functions?
Posted
Updated 26-Sep-19 1:09am
v2
Comments
Arun Jacob 23-Jul-10 0:38am    
Reason for my vote of 1
Try some online resources/books.

The main advantage of virtual functions are that they directly support object oriented programming. When you declare a function as virtual you're saying that exactly what code is executed depends on the type of the object you call it against.

The main disadvantage of virtual functions is the converse of the above: It can be hard to reason about what your code is doing just by reading it. So if you see a function:

void foo( interface_ptr *p )
{
    p->a();
    p->b();
}


you can't tell exactly what code path it's going to follow.

If you're concerned about performance then don't be. Even luddite games programmers are now using virtual functions and finding the performance isn't that bad and usually beats hand rolling polymorphism other ways.

Cheers,

Ash
 
Share this answer
 
Virtual fucntion are a must to support runtime polymorphism.

C++
class A
{
public:
   virtual ~A() {}
   virtual void hallo()
   { std::cout << "I'm A[" << this << "]" << std::endl; }
};

class B: public A;
{
public:
   virtual void hallo()
   { std::cout << "I'm B[" << this << "]" << std::endl; }
};

int main()
{
    static const size_t N = 4;
    A* v[N];

    for(size_t i=0; i<N; i+=2)
    {
       v[i]   = new A;
       v[i+1] = new B;
    }

    for(size_t i=0; i<N; ++i)
       v[i]->hallo();

    for(size_t i=0; i<N; ++i)
    { delete v[i]; v[i]=0;  }

    return 0;
}


Even if main manages just A* (and new B returns a B* that automatically convert into A* being B derived from A), making hallo and the destructor virtual makes v[i] (that is an A*) when calling hallo orwhen under delete to call the most derived method.

This is not possible without virtual functions, unless storing tales of function pointers that are noting more than the compiler generate v-tables.

The program output should be:
txt
I'm A[0xxxxxxx]
I'm B[0yyyyyyy]
I'm A[0zzzzzzz]
I'm B[0wwwwwww]
 
Share this answer
 
You might want to go through some books on OOPS concept while you are reading about virtual methods.
 
Share this answer
 
>If a class is inherited from a class containing a virtual function, then the function definition can be redefined in the inherited class.
If a function is virtual and a new definition of the function is given in a derived class, then for any object of the derived class, that object will always use the new definition. Even if it is invoked in the inherited function.
Virtual functions takes much space as virtual function table is created by compiler which contains pointers for each virtual member function which points to the memory location of code for that function.
Programs containing Virtual functions run slow.
 
Share this answer
 
Comments
Emilio Garavaglia 23-Jul-10 12:30pm    
Reason for my vote of 1
The most of the issues about space and speed are resolved by compiler optimization.
In 2010 be afraid of virtual functions is simply a non-sense.
virtual functions plays an important role in all framework designs.
 
Share this answer
 
Comments
Emilio Garavaglia 23-Jul-10 15:42pm    
Reason for my vote of 2
irrelevant respect to the question
Aescleal 23-Jul-10 16:52pm    
Reason for my vote of 1
Not necessarily - at least one framework use the Curious Recurring Template Pattern to avoid using virtual functions.
This is a very basic question, Better you learn some basics.

Virtual keyword allows a method to be overridden in the inherited class.

Virtual C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900