C + + Fundamentals: Virtual Functions, overloads, overrides, Hide < go >

Source: Internet
Author: User
Tags what interface

Transferred from: http://www.2cto.com/kf/201404/291772.html

Virtual functions are always associated with polymorphism, and virtual functions can be used to manipulate inherited class objects using a base-class pointer!

Virtual Functions: Inherited interfaces (function names, parameters, return values), but implementations do not inherit (body of functions)

Non-virtual function: Inherit the interface, also inherit the implementation;

1) virtual destructor (when a class is intended to be used as a base class, its destructor must be a virtual function)

Can a constructor be a virtual function? No, you must explicitly specify to the compiler what type of object to generate when generating the object, so there is no problem with the virtual function, only if the object already exists, what interface do I use to manipulate it;

Example:

Class A
{
Public
A ();
Virtual~a ();
};
Class B:public A
{
Public
B ();
~b ();
};
int main ()
{
A *pa=new B; Call B's Constructor
Delete PA;
return 0;
}

Here through pointer A to call the Class B destructor, but if ~a () is not a virtual destructor, there is no polymorphism, it will call the destructor in a, the final result may be B object in a component is destroyed, the other remains; so when a class is a base class, its destructor must be a virtual function. This prevents the occurrence of incomplete destruction;

2) When a virtual function appears in a construct or destructor (that is, a virtual function is called in a constructor), the function is degenerate into a normal function. Why? Terms of <>

Example: (Transfer from <> to Regulation 9)

Class Transaction

{
Public
Transaction ();
virtual void logtransaction () const = 0;
// ...
};
Transaction::transaction ()
{
// ...
Logtransaction ();
}
Class Buytransaction:public Transaction

{
Public
virtual void logtransaction () const;
// ...
};

The constructor for Buytransaction is called here, because Buytransaction is the inheriting class, so the constructor for the base class is called first, when the part that is unique to the derived class is not initialized in the,<>, so to say:" The Buytransaction component in this object is not initialized, the safest way is that when he does not exist, the object does not become a derived object when the derived component is not initialized " so the transaction constructor calls the base class's Logtransaction () const; And here is a pure virtual function that will error

3) virtual function with overlay, reload, hide

Overloads first appear in the non-inheritance relationship, when the same class, two function parameters, the same name, the return value type does not matter (function return value is not used as an overloaded reference, because the function call does not appear when the return value);

Note that two functions can be overloaded simply because of the difference between const and non-const;

Virtual functions and overrides are present in an inheritance system, overriding the common function, which requires absolute consistency when the same name appears in the parent subclass (same return value, same parameter, same function name).

virtual function in the parent class subclass, first declared in the parent class that the function is virtual, then the subclass can redefine the implementation of the function, here is mainly related to polymorphism, is the case of coverage plus virtual, through pointers or references to achieve polymorphism;

Hiding appears simple and rude, in the inheriting class as long as the parent class is the same name (only requires the same function name/variable name, other return value, virtual non-virtual) functions, when the object called with the same name function, the base class corresponding function, variable hiding, If a member with the same name to access its parent class should explicitly use the base class name:: Member to access;

Example:

Class A
{
Public
Virtual ~a () {};
void process (int i,char c); overload that determines which function is called during compilation
Char process (double d,int c); As long as the function name is the same, return does not matter, parameter different
void process (int i);
void process (int i) const; This is equivalent to void process (const A *this,int i);
void process (Consttint i); Overloading can be implemented here only because of the const attribute being different
virtual int foo (int, char) {...};
int Foo2 () {};
void Foo3 (Int,int) {};
}
Class B:public A
{
Public
int foo (int, char) {...}; Virtual functions, where the function interfaces are strictly consistent (most compilers require a consistent return value)
int process () {...}; Hide base class functions
int Foo2 () {}; This does not contain a virtual property, it will overwrite
int Foo3 () {return 0;}; Will hide the Foo3 of Class A.
}
int main ()
{
b b;
A *pa=&b;
Pa->foo2 (); This invokes the Foo2 () of Class A, and does not involve polymorphism;
Pa->foo (3, ' C '); Class B foo () involves polymorphic
}

Summarize:

1) Overlay and virtual function is a pair of brothers, requires the function of the return value, function name, parameters strictly consistent, virtual function is overlay plus virtual case;

2) Hide is the extension of coverage, covering is a hidden exception, only requires the function name, other regardless, in the inheritance system, the subclass of the same name function will be the parent class of the same name functions hidden;

3) When the pointer is used, pointers to the base class are executed according to the actual object type, and the virtual function of the base class is still executed if the derived class does not redefine the virtual function of the base class;

4) When a virtual function is not present, a pointer to the base class is not dropped into the derived class to search for the function, so the virtual property is equivalent to telling the base class pointer: When executing me, go to the corresponding object to search for the corresponding virtual function;

5) Pure virtual function is the class is abstract class, can not be instantiated, the definition of pure virtual function means that this function can only be the parent class, which is responsible for defining the interface and not responsible for implementation;

6) When using the object to invoke the corresponding function, the main consideration is the function of the same name of the base class hidden (including overwrite), and do not need to consider polymorphism;

7) Notice that the virtual function cannot be called in the structure destructor, when the constructor of the derived class is executed, the base class component is constructed, and then the structure of the derived class component is executed, and the virtual function is meaningless at this time.

8) C + + is a very complex language with a lot of details;

C + + Fundamentals: Virtual Functions, overloads, overrides, Hide < go >

Related Article

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.