Advertisement

Virtual Void ?? what does it mean ?

Started by November 16, 2000 04:18 AM
4 comments, last by Havana Smurf 24 years, 2 months ago
Sory guys but i have to know what a ''Virtual Void'' function does - what does the ''Virtual'' bit do to a function. Does it mean that it is defined in another file ? Im a bit stuck cause i dont know how to use it so help would be appreciated. Thanx
''Once a smurf, always a smurf''
quote: Original post by Havana Smurf

Sory guys but i have to know what a ''Virtual Void'' function does - what does the ''Virtual'' bit do to a function.

Does it mean that it is defined in another file ?

Im a bit stuck cause i dont know how to use it so help would be appreciated.

Thanx


The void keyword stands for "a function which returns nothing". Here''s an example :

  void print_copyright_infos(){	cout<<"(c) LOADED"<<endl;	cout<<"www.loaded.fr.st"<<endl;}   


The virtual keyword is a bit more complex. It only applies to member functions of a class and is used for inheritance. If your function is virtual then you can overload it in the class childs.

If you need more precise informations, just send me a mail.
Advertisement
The virtual keyword is usually used to define functions in a base class. What it means is that the function might be redefined in a derived class. Check the Help system that comes with MSVC++ for more help


This RtS-Babble© has been brought to you by:
-Run_The_Shadows
-Run_The_Shadows@excite.com
BTW, it is not used in inheritance (except when it''s null) but only when you polymorphs the class.

I always understand better with an example, so I''ll give you one:
  #define EOL (''\n'')class Base{public:   virtual void Toto() { cout << "Base->Toto" << EOL; };     virtual void Tata() = 0;                // A null function.   void Titi() { cout << "Base->Titi" << EOL; };  // A non-virtual function.};class DerivedA: public Base // This class is valid.{public:   virtual void Toto() { cout << "A->Toto" << EOL; };   virtual void Tata() { cout << "A->Tata" << EOL; };   void Titi() { cout << "A->Titi" << EOL; };};class DerivedB: public Base // This class is invalid{public:   virtual void Toto() { cout << "B->Toto" << EOL; };   void Titi() { cout << "B->Titi" << EOL; };};void main(){   Base* pB = new DerivedA; // Polymorphs...   DerivedB B;   pB->Toto();  // Output "A->Toto"   pB->Tata();  // Output "A->Tata"   pB->Titi();  // Output "Base->Titi"   pB.Toto();  // Output "A->Toto"   pB.Tata();  // Output "A->Tata"   pB.Titi();  // Output "A->Titi"   pB = new Base; // Generate error...}  


The DerivedB class is invalid because you HAVE to derive the member Tata. Since it is null, you always have to derive it.
The "pB->Titi();" line prints "Base->Titi" because Titi was not virtual in first place. This is only used with polymorphism.
The "pB = new Base" is invalid because you cannot make an instance of a class which contains null functions.

You could have made a derived class with null functions, but in this case you wouldn''t have been able to make a reference to it, only inherit it.

This may seem a bit complex, but once you use these concepts, you find them pretty simple.
Now I know what I'm made of, and I'm afraid of it...
The function Tata is known as a pure virtual function in C++.

TheViper
quote: Original post by TheViper

The function Tata is known as a pure virtual function in C++.

TheViper


You right I have to study back those C++ terms.
Now I know what I'm made of, and I'm afraid of it...

This topic is closed to new replies.

Advertisement