What is the function of C + + virtual function?

Source: Internet
Author: User

In "Easy to understand MFC" Describes virtual function is to perform generalized operation, has not grasped the main points. Now the understanding is abstract, the previous consideration of the problem is from the abstract to the image , such as the example below, from the upper (virtual base class "monster") to the bottom (three subclass of the derived class "Wolf" Spider "Python") to consider. In fact, reverse thinking, from the figurative to abstract , the role of virtual function is obvious, will be different but also a similar kind of behavior extracted into an abstract virtual function in the base class, and then perform a generalized operation.

What is the function of C + + virtual function?

7sDream, two new inhabitants/software enthusiasts/0 answers to the annihilationArana Auron, Tian Shi fly, acid swamp slime monsters and other people agree May give examples better understand the saying ~ ~
Running this will output "A".
----------------------------------------------------------------------------------
This will output "B".
--------------------------------------------------------------------------
Add: Above two because it is a diagram I will not change ~ @vczh wheel Brother teaches us that the destructor of a is also added to virtual, or delete p when the change of A's destructor may be a problem ~ ~
--------------------------------------------------------------------------
In short, it is
When a base-class pointer is directed to a subclass object, which invokes the subclass and the base-like name member function by this pointer, the base class declares the virtual function "subclasses do not write or can" to tone the function of the class, and the base class is called without declaring it. Feel it for yourself 0.0
--------------------------------------------------------------------------
The situation that needs to be used is probably this:
For example, you have a game, there is a virtual base class called "Monster", there is pure virtual function "attack." Then derived three sub-categories "Wolf" Spider "Python", have achieved their own different "attack" function, such as the wolf is biting, the spider is the silk, the python wrapped you up ~ ~
And then there are a lot of monsters that can define aan array of virtual base-class pointers, give the various monster pointers to it, and iterate loopsWhen the direct monster[i]->attack () attack the player on the line, probably see:
If there is no virtual function, it is more troublesome to manage a bunch of objects of similar type but different types.
---------------------------------------------------------------
Freshman level for Guidance ~ ~ Don't know say right ~ ~ also has code format, variable name God horse's all seek guidance ~0.0~ @vczh

Vczh the rest is good, except for A's destructor is not virtual ... So you delete P is not going to execute the B's destructor2014-05-31 7sDream(author) reply Vczh OwO, Know ~ ~ Learned the time to talk about this problem ~t^t now forget ... I continue to study to be good ~
Yes, there is a question ~ ~ Today I heard that the MFC framework design is not very good ... Well, do I have to learn to pinch? What do you want to learn instead of pinching 0.0~ if you don't learn?2014-05-31 Vczhreply 7sDream(author) Do GUI of course to learn C # with WPF, and so you get used to, later need to use what framework to switch is not a problem. MFC is too old for new projects to be used.2014-05-31 7sDream(author) reply Vczh 3q~~ Good ~ ~ Small semester Self-study to ~ one months of no class but must stay in school time, should be not bored ~\^0^/2014-05-31 The pointer to the Liu Hanqui class points to the object of the derived class, and the pointer can only invoke the member function defined by the base class, and if the derived class defines a virtual function, this pointer to the derived class's base class is called a member function of the derived class.2014-06-03 Gao Hongtao is such a manager!

What is the function of C + + virtual function?

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.