Valid tive C ++ 6

Source: Internet
Author: User

Virtual function: the interface must be inherited.

Non-virtual: interfaces and implementations are inherited.

Public inheritance: liskov substitution principle

The base can be replaced by derived.

If function parameters use point or reference to pass objects.

The key to public inheritance: is-a model.

The name of derived-class hides the name of base-class.

Example:
Class base <br/>{< br/> Public: <br/> virtual void mf1 () = 0; <br/> virtual void mf1 (INT ); <br/> virtual void mf2 (); <br/> void mf3 (); <br/> void mf3 (double); </P> <p> }; <br/> class derived: public base <br/>{< br/> Public: <br/> virtual void mf1 (); // mf1 masks all base-name mf1 functions. <Br/> void mf3 (); // mf3 masks all functions named mf3 in the base. <Br/> void mf4 (); <br/>}; <br/>
Solution:

1. Declare using base: mf1;

If private inheritance points to one mf1 that inherits the base class, you can use the transfer function

Virtual void mf1 ()

{

Base: mf1 ();

}

 

Pure virtual function declares in base that it must be defined in derived, that is, it must provide its own version; otherwise, an error is returned.

The purpose of pure virtual is to inherit interfaces.

Note that pure virtual can also be defined. You can implement overwriting and inherit using conversion functions.

Impure virtual aims to inherit interfaces and default implementations. If default inheritance is not defined, it can be overwritten by derived.

 

80-20 Rules make sure that we pay attention to 20% of the Code, because they use 80% of the time.

 

Non-virtual inherited interface and forced implementation.

1. template method design mode:

The private virtual function called by the Public Non-virtual member function is called non-Virtual Interface (nvi)

The non-virtual function is called an overlay.

Advantage: You can do additional work first and later.

Derived class can be used to redefine the inheritance of private virtual functions.

2. Implement the Strategy Mode Using function pointer

The constructor parameter accepts a pointer.

3. tr1: function implements the Strategy Mode.

For example, typedef STD: tr1: function <int (const A &)> H; // H is any callable object.

// The signature indicates the function that accepts a reference pointing to const A and returns an int.

// The design is compatible.

As long as the H parameter can be converted to const & A, the returned value can be converted to the int function.

4. Traditional Strategy Mode

Convert the original virtual function into a class.

 

No derived class should redefine the non-virtual function of the base class.

 

Virtual is dynamic binding, and the default parameter value is static binding.

 

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.