By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Virtual functions clarifications

100+
P: 180
Hi guys,
I have problem understanding virtual functions??? Like how it works.

My friend asked me some questions

1) What is a static function???
I replied functions to be used without intantiating an object of that class.

2) Can static function be called using this pointer???
I told no!!! but I dont know why??? This is because my funda is not clear.
Need some exaplantion about why???. If possible using day to day life example as like Ganon11 (where I say his explanation for pure virtual functions). It was really good one.

3) What is virtual function???
I know the defination (from book) but the funda is not clear. Please explain with day to day life example .

4) Can virtual functions be made static????

5) Can i call a virtual function from within a constructor????

These are some of the questions I have and I need to make them clear. Please help

Thanks in advance
Mar 15 '07 #1
Share this Question
Share on Google+
14 Replies


P: 4
A short reply to some questions (question 3 is quite long to be properly answered, and you can find good explanations both on the web and on books).

1) Your answer is right. And you can use only static and local variables within a static function.

2) Yes, they are class members. But you just have to try to find out.

3) Short answer: they are functions which a derived class can redefine. Then at runtime the "most new" version (i.e., those of the derived class) of the function will be called (unless you explicitly specify which one you are going to call), no matter from where the function is called.
Pure virtual functions (with =0 at the end of the function declaration) are the same as virtual functions, but you don't define them in the base class. You just declare them. Therefore you cannot instantiate objects of the base class, since there are undefined functions.

But there are more details to find out, just google for virtual functions and pure virtual functions.

4) No, I don't know exactly why. But again, just try and find out.

5) Obviously yes, it is a function like all the others.

Sandro
Mar 15 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
3) Short answer: they are functions which a derived class can redefine. Then at runtime the "most new" version (i.e., those of the derived class) of the function will be called (unless you explicitly specify which one you are going to call), no matter from where the function is called.
Actually, they are functions which a derived class must define. Any derived class can override any base class's function, but it has the option of doing so. Declaring a function as virtual requires subclasses to redefine the function.

At runtime, the 'most new' version is not necessarily the one that is called. For instance, suppose you have two classes, Base and Derived. In Base, you declare a virtual function called function(). Thus, Derived must have function() also. The way you answered, the Derived::function() would always be called, since it is the "most new". But if you have an object of type Base and you call function(), the Base::function() is used. When you call function() on any Base or Derived object, the computer determines which class the object is and uses the appropriate function.
Mar 15 '07 #3

100+
P: 180
For instance, suppose you have two classes, Base and Derived. In Base, you declare a virtual function called function(). Thus, Derived must have function() also
Are you sure this statement is correct. If yes

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.   public:
  4.           A(){}
  5.           virtual void func()
  6.           {
  7.              cout<<"A's func()"<<endl;
  8.           }
  9. };
  10.  
  11. class B : public A
  12. {
  13. public:
  14.           B(){}
  15. }; 
  16.  
  17. void userFunc( A* obj )
  18. {
  19.    obj->func();
  20. }
  21.  
  22. int main()
  23. {
  24.    A a;
  25.    B b;
  26.    userFunc( &a );
  27.    return 0;
  28. }
Here class B derived from A doesnt have a function func();

I'm totally confused now....Please guys....
Mar 15 '07 #4

dmjpro
100+
P: 2,476
hello if the derived class don't override the base class function then the derived class uses the base class version.........
Mar 15 '07 #5

100+
P: 180
hello if the derived class don't override the base class function then the derived class uses the base class version.........
Hi dmjpro,
Can you please be more specific??? If possible with example given in above post.

Thanks in advance
Mar 15 '07 #6

dmjpro
100+
P: 2,476
Hi guys,
I have problem understanding virtual functions??? Like how it works.

My friend asked me some questions

1) What is a static function???
I replied functions to be used without intantiating an object of that class.

2) Can static function be called using this pointer???
I told no!!! but I dont know why??? This is because my funda is not clear.
Need some exaplantion about why???. If possible using day to day life example as like Ganon11 (where I say his explanation for pure virtual functions). It was really good one.

3) What is virtual function???
I know the defination (from book) but the funda is not clear. Please explain with day to day life example .

4) Can virtual functions be made static????

5) Can i call a virtual function from within a constructor????

These are some of the questions I have and I need to make them clear. Please help

Thanks in advance

2.because this is silently generated when the object is created .... but in the static function there is no surity of object creation..... so u can't access this from static function.

3.virtual functions are those function which are bound by the run time system at run time.....
if u don't specify a method as virtual then compiler decides to run at compilation time.....

the virual function is manatained by VPTR which silently inserted by Compiler and resolved by run time system

know one thing the static funciton never be inherited so it is virtual does not make any sense.....

u can't call virtual function inside constructor .. because inside the constructor the VPTR mechanism inserted.... i think it may work ... but it is not safe

welcome .....
Mar 15 '07 #7

100+
P: 180
Wow!!! This is what I wanted... Thanks a lot

I have some more doubts from above.

virtual functions are those function which are bound by the run time system at run time.....
if u don't specify a method as virtual then compiler decides to run at compilation time
So you mean to say, member functions are selected based on type of object rather than pointer/reference to that object.

I would be happy if you could explain me how this VPTR works. Suppose I have a class Base and a class Derived. There are two virtual functions defined in class Base, these two methods are overridden in Derived class. How is that the VPTR comes into picture????

know one thing the static funciton never be inherited so it is virtual does not make any sense.....
This is really useful NOTE for me...

1) Another question!! I know what a virtual destructor is.. I would like to know what is the use of pure virtual destructor??? Is it necessary that for virtual destructor there has to be a virtual constructor????

Thanks in advance
Mar 15 '07 #8

dmjpro
100+
P: 2,476
the VPTR is a pointer to the all virtual functions of that class....

the run time system will get the run time actual object type.....

then from that address the VPTR is retrieved and take the decision which function version will get called ....

m i clear .......
Mar 15 '07 #9

dmjpro
100+
P: 2,476
know one thing ... i started after totally detached from C++ for one year...

so right now i can't clearify the destructor .. actually i am working on J2EE

but after 10-15 days i wil be as i was before one year ......

okkkkkk... so look for another expert and have a good day......
Mar 15 '07 #10

Banfa
Expert Mod 5K+
P: 8,916
Actually, they are functions which a derived class must define. Any derived class can override any base class's function, but it has the option of doing so. Declaring a function as virtual requires subclasses to redefine the function.
Err actually Ganon this just isn't true. I did have to look this up to be sure about it but from section 12.2.6 of "The C++ Programming Language" by B. Stroustrup
A virtual function can beused even if no class is derived from its class, and a derived class that does not need its own version of a virtual function need not provide one. When deriving a class, simply provide an appropriate function, if it is needed.
So a class is not required to redefine a function that is declared virtual in one of its parent classes, but it may if it needs to.

The big difference is that when a function is declared virtual you are guaranteed that the correct function for the object will be called regardless of the type of the reference it is called through.

As illustrated in the difference between PrintMe and PrintMsg in the program below. PrintHelloWorld is an example of a non over ridden virtual function.

Note:

If you fail to override a pure virtual function inherited from an abstract parent class then the derived class will also be abstract since it will contain a pure virtual function.

If you declare any functions virtual it is good practice to declare the destructor virtual too.

Expand|Select|Wrap|Line Numbers
  1. #include "iostream"
  2. using namespace std;
  3.  
  4. class Base
  5. {
  6. public:
  7.     virtual void PrintMe(){ cout << "Base" << endl; }
  8.     virtual void PrintHelloWorld(){ cout << "Hello World" << endl; }
  9.     void PrintMsg(){ cout << "I'm in Base" << endl; }
  10.  
  11.     Base(){};
  12.     virtual ~Base(){};
  13. };
  14.  
  15. class Derived : public Base
  16. {
  17. public:
  18.     virtual void PrintMe(){ cout << "Derived" << endl; }
  19.     void PrintMsg(){ cout << "I'm in Derived" << endl; }
  20.  
  21.     Derived(){};
  22.     virtual ~Derived(){};
  23. };
  24.  
  25. int main()
  26. {
  27.     Derived d;
  28.     Base &rb = d;
  29.  
  30.     rb.PrintMe();
  31.     rb.PrintHelloWorld();
  32.     rb.PrintMsg();
  33.  
  34.     return 0;
  35. }
  36.  
Mar 15 '07 #11

Ganon11
Expert 2.5K+
P: 3,652
Err actually Ganon this just isn't true.7
Hrm. I, also, had to look this up to make sure, but you're right. I guess that was a leftover confusion from working with abstract in Java, where there was no 'virtual' option, only the equivalent of pure virtual. Sorry for any confusion caused.
Mar 15 '07 #12

100+
P: 180
Firstly thanks to dmjpro for a quick and comforting reply.

Hi Banfa,
Thanks for your satisfying reply. Now my concept is becoming more clearer.

But still this questions are to be answered..

1)I would like to know what is the use of pure virtual destructor???

2)About VPTR. dmjpro has already explained about it in the previous post but still I'm unclear and has not got a clear picture.

Could you please help me to understand above two questions.

Thanks
Mar 16 '07 #13

dmjpro
100+
P: 2,476
look at this code what compiler inserts silently......

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.    void * VPTR = holds the address of the off all virual funations of class A
  4.    //some code
  5.    virtual void fun()
  6.    {
  7.       //Class A function fun
  8.    }
  9.    //some code
  10. };
  11.  
  12. class B : public A
  13. {
  14.    void * VPTR = holds the address of the off all virual funations of class B
  15.    //some code
  16.    virtual void fun()
  17.    {
  18.       //Class B function fun
  19.    }
  20.    //some code
  21. };
  22.  
  23. //some code .....
  24. B b;
  25. A a;
  26. A *ap = &b;
  27. ap->fun();//here now ap holds the address of class B object and the corresponding VPTR retrieved and actual adress of fun function retrieved and gets called
  28.  
  29. //similarly solved if ...... ap = &a and ap->fun();
okk .... i think now u got my point .....

welcome again...... have a good day...
Mar 16 '07 #14

100+
P: 180
Thanks.... Now it gives a clear picture.

Thanks again
Mar 16 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.