Deep understanding of the Virtual keyword _c language in C + +

Source: Internet
Author: User

What is the main function of the 1.virtual keyword?
function calls in C + + default do not apply to dynamic bindings. To trigger a dynamic binding, two conditions must be met: First, the specified as a virtual function, and secondly, by a reference or pointer to the base class type.
This shows that virtual main is mainly to achieve dynamic binding.

2. Can I use the virtual keyword in those situations?
Virtual can be used to define class functions and apply to virtual inheritance.

The static static function of a friend function constructor cannot be modified by the virtual keyword;
Ordinary member functions and destructors can be modified by virtual keyword.

Effects of 3.virtual functions

Copy Code code as follows:

Class grandfather
{
Public
Grandfather () {}
virtual void Fun ()
{
cout << "grandfather call function!" << Endl;
}
};

Class Father:public Grandfather
{
Public
Father () {}
void Fun ()
{
cout << "Father call function!" << Endl;
}
};


Class Son:public Father
{
Public
Son () {}
void Fun ()
{
cout << "Son call function!" << Endl;
}
};

void print (grandfather* father)
{
Father->fun ();
}

int _tmain (int argc, _tchar* argv[])
{
Father * pfather = new Son;
Pfather->fun ();
Grandfather * Pgfather = new Father;
Print (Pgfather);
return 0;
}


Output is Son call function
Father Call function

The inheritance of 4.virtual
As long as the base function defines virtual, the function of the inheriting class also has the virtual attribute
That is, the grandfather Father son defines virtual void fun () and grandfather a definition of virtual void fun effect is the same

5. Virtual destructor

Copy Code code as follows:

Class grandfather
{
Public
Grandfather () {}
virtual void Fun ()
{
cout << "grandfather call function!" << Endl;
}

~grandfather ()
{
cout << "grandfather destruction!" << Endl;
}
};

Class Father:public Grandfather
{
Public
Father () {}
void Fun ()
{
cout << "Father call function!" << Endl;
}

~father ()
{
cout << "Father destruction!" << Endl;
}
};


Class Son:public Father
{
Public
Son () {}
void Fun ()
{
cout << "Son call function!" << Endl;
}

~son ()
{
cout << "Son destruction!" << Endl;
}
};

void print (grandfather* p)
{
P->fun ();
}

int _tmain (int argc, _tchar* argv[])
{
Father * pfather = new Son;
Delete Pfather;
return 0;
}


Above code output: Father destruction!
Grandfather destruction!
The constructor of son is executed and the destructor of son is not executed, so the destructor of grandfather is set to virtual
Then output: Son destruction!
Father destruction!
Grandfather destruction!

6. Pure virtual function
Pure virtual functions are defined as follows:

Copy Code code as follows:

Class grandfather
{
Public
Grandfather () {}
virtual void fun () = 0
{
cout << "grandfather call function!" << Endl;
}

Virtual ~grandfather ()
{
cout << "grandfather destruction!" << Endl;
}
};


Pure virtual functions provide an accessible interface for descendant classes, but the version in this class is never invoked.
A class that contains (or continues) one or more pure virtual functions is an abstract base class, and an abstract base class cannot be instantiated!
Inheriting classes can be instantiated only if you override this interface


7. Virtual Inheritance
Virtual inheritance mainly solves the problems caused by cross inheritance. Here gives a reference article C + + virtual inheritance.
Give an example as follows

Copy Code code as follows:

Class grandfather
{
Public
Grandfather () {}
void Fun ()
{
cout << "grandfather call function!" << Endl;
}

Virtual ~grandfather ()
{
cout << "grandfather destruction!" << Endl;
}
};

Class Father1:public Grandfather
{
Public
Father1 () {}
void Fun ()
{
cout << "Father call function!" << Endl;
}

};

Class Father2:public Grandfather
{
Public
Father2 () {}
void Fun ()
{
cout << "Father call function!" << Endl;
}

};


Class Son:public Father1, public Father2
{
Public
Son () {}
void Fun ()
//{
cout << "Son call function!" << Endl;
//}
};

void print (grandfather* p)
{
P->fun ();
}

int _tmain (int argc, _tchar* argv[])
{
son* Son = new Son;
Son->fun ();
return 0;
}


The error is prompted at compile time to fun access is ambiguous
This problem can be resolved if both Father1 and Father2 inherit the grandfather class with virtual inheritance

8. Virtual functions in constructors and destructors
If a virtual function is invoked in a constructor or destructor, the version defined for the constructor or destructor's own type is run


9. Realization mechanism of virtual function
About the realization mechanism of virtual function, we introduce later.

10. Summary
On the use of Virtual keyword Summary as above, there are errors or summary is not in place, please help me to point out!

11. Examples

Copy Code code as follows:

Class ClassA
{
Public
ClassA ()
{
Clear ();
}
Virtual ~classa ()
{
}
void Clear ()
{
memset (this, 0, sizeof (*this));
}
virtual void func ()
{
printf ("func\n");
}
};

Class Classb:public ClassA
{
};

int main (void)
{
ClassA OA;
CLASSB ob;
ClassA * pa0 = &oa;
ClassA * PA1 = &ob;
CLASSB * PB = &ob;
Oa.func (); 1
Ob.func (); 2
Pa0->func (); 3
Pa1->func (); 4
Pb->func (); 5
return 0;
}


To add an example, the output of this program is sequentially
Func
Func
Error
Func
Func

To talk about my understanding when
ClassA OA;
Oa.func ();
There is no process of dynamic invocation, so func is a virtual function, but function calls are not accessed through virtual tables, so even

Copy Code code as follows:

memset (this, 0, sizeof (*this));

It doesn't matter if the virtual table address is not found
When performing CLASSB ob, note that memset is the ClassA address, and that all OB virtual tables exist
That is, accessing the Func function of OA via pointers or references (dynamic binding) (which requires access from the virtual table), error
Access to the Func and functions of OB, regardless of static or dynamic access, there will be no error


When you change the CLASSB code to the following

Copy Code code as follows:

Class Classb:public ClassA

<pre style= "Font-weight:bold" class=cpp name= "code" >{</pre><pre style= "Font-weight:bold" class=cpp Name= "Code" > ClassB ()
{
Clear ();
}
Virtual ~CLASSB ()
{
}
void Clear ()
{
memset (this, 0, sizeof (*this));
}</pre><br>
<PRE></PRE>
<pre style= "Font-weight:bold" class=cpp name= "code" >};</PRE> output is


Func
Func
Error
Error
Error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.