What are virtual functions

Started by
5 comments, last by kire 17 years, 7 months ago
Hi mates, Anyone can describe what are virtual functions are ? And what's this code problem : #include <iostream> #include <string> using namespace std; class Base{ public: virtual void a(){ cout<<"Base ran\n"; } }; class Drived:public Base{ public: void a(){ cout <<"Drived ran\n"; } }; void main(){ Drived* b=&Base b->a(); } Error : Error 1 error C2275: 'Base' : illegal use of this type as an expression Thx
Advertisement
You use the type of the class as a variable and it isn't.
You have to write:
Drived* b = new Base();
Your code's problem is that you tried to assign a TYPE to a VARIABLE. This can't be done. First, you have a declare a variable of type Base and then you can assign the address of this variable to your Derived pointer.

To answer your first question, a virtual function of class A is a function whose implementation can be overridden in any class that inherit class A. To know these functions, the compiler have to mark them (hence the virtual keyword). For example:

class A{public:  virtual void f1() { std::cout << "A::f1" << std::endl; }  void f2() { std::cout << "A::f2" << std::endl; }};class B : public A{  virtual void f1() { std::cout << "B::f1" << std::endl; }  void f2() { std::cout << "B::f2" << std::endl; }};int main(){  A a;  B b;  A *p = &b  a.f1(); // (1}  b.f1(); // (2}  a.f2(); // (3}  b.f2(); // (4}  p->f1(); // (5)  p->f2(); // (6)}

(1): we simply call the f1() method on an object of type a - we'll print A::f1
(2): same, we call f1() on an object of type B - we'll print B::f1
For now, we haven't used the virtual function mechanism explicitely. In fact, there have been an implicit use but its effect on the output can't be seen.

(3): pretty obvious: A::f2
(4): also pretty obvious: B::f2

Now, p is an object of type B in disguise - we use it as if it was an object of type A.

(5): since f1() is virtual, we won't call A::f1() but B::f1() (hence we'll write B::f1)
(6): even if the real type of the object behind p is B, f2 is NOT virtual - thus we'll call the corresponding method in A, not in B. A::f2.

From a technical point of view, an instance of a polymorphic object (ie an object that have at least one virtual method) keeps track of its virtual method in order to be able to call the correct one when needed. He does this using what is often called a vtable (or vtbl), which is a short for "virtual function table".

Regards,
Thx for your replying
I think i am learning C puls plus in here !
>:D<
Quote:Original post by bigmantana
Thx for your replying
I think i am learning C puls plus in here !
>:D<


You should consider to actively participate to the gdnet C++ workshop (see signature for the link). It will only require you to buy a cheap book.

C++ is a great language, but it is also very difficult. It requires time and dedication. I hope we'll be able to help you to achieve your goals.

Regards,
so is a virtual function in C++ the same as an abstract method in Java then?
Quote:Original post by samoz
so is a virtual function in C++ the same as an abstract method in Java then?


not at all.

An abstract method (or "pure virtual") in c++ would look like this:

virtual void abstractMethod() = 0;

The virtual is the keyword that helps making polyformism in c++ easier. The decent explanation stands above ;).

[Edited by - kire on September 4, 2006 1:56:41 PM]

This topic is closed to new replies.

Advertisement