Generic programming Basics

Source: Internet
Author: User

1. The C ++ template provides the compilation phase polymorphism capability. Please explain this.

When we consider polymorphism in the face-to-face world, we consider Runtime polymorphism, which comes from virtual functions. The base class creates an interface "contract", which consists of a system virtual function. The derived class can be derived from the base class, and rewrite the virtual functions of the base class without violating the semantics of the contract. In this way, the code that expects to work with a base class Object (using a pointer or reference to hold a base class Object) can also run well using a derived class.

However, Runtime polymorphism has two main disadvantages: first, these types must be located in the class hierarchy inherited from a certain public base class; second, when a virtual function is called in an intensive loop, you may notice that it brings some runtime overhead, generally, each call to a virtual function requires an extra indirect layer virtual function pointer. At the same time, the compiler is responsible for distributing virtual function calls to the corresponding functions in the derived class according to your meaning.

2. What is this doctrine? Make sure that you have two template parameters instead of one.

Template <class T1, class T2>

Void construct (T1 * P, const T2 & value)

{

New (p) T1 (value );

}

The construct () function template constructs an object at a given memory location and initializes it with an initial value. Here, the new operator is called the position new (placement New). In this form, the new operator does not allocate memory for the new object, but places the new object to the memory position indicated by P. Any new object in this way should be destroyed by explicitly calling its destructor, rather than using Delete

So why does construct () have two template parameters? Because construct () has only one parameter, if you want to copy from a different type of object, You Have To explicitly specify the parameter type.

For example, there is only one parameter:

Template <class T>

Void construct (T * P, const T & value)

{

New (p) T (value );

}

// Assume that P1 and P2 all point to uninitialized memory

Void F (double * P1, base * P2)

{

Base B;

Derived D;

Construct (P1, 3.33); // OK

Construct (P2, B); // OK

Construct (P2, 3); // error: Is the T type double or Int?

Construct <double> (P2, 3); // OK

Construct (P2, d) // error: Is T1 base or derived?

Construct <base> (P2, d); // OK

}

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.