"Deep Exploration C + + object Model" Reading notes (7)

Source: Internet
Author: User

Template's "Present" behavior * * * *

Any member in the template class can only be accessed or manipulated by an entity in the template class.

Point<float>::Status s;  // ok
Point::Status s;  // error

If we define a pointer, point to a specific entity, like this:

Point<float> *ptr = 0;

Since this is a pointer to class object and is not a class object in itself, the compiler does not need to know any members data associated with that class. So it is not necessary to have a float entity of point.

If it is not a pointer but a reference, suppose:

Point<float> &ref = 0;

The true semantics of this definition will be extended to:

// 内部扩展
Point<float> temp(float(0));
Point<float> &ref = temp;

The above conversion is because reference is not synonymous with nothing (no object), 0 is considered an integer and must be converted to an object of type point<float>.

However, member functions only when member functions is in use, C + + standard requires them to be "present". There are two main reasons for this rule:

(1) Space and efficiency considerations. It will take a lot of time and space for the unused function to be "present".

(2) features that have not yet been implemented. Not a template all types must be able to fully support a group of member functions, and thus need only a member functions that is really needed.

As an example:

Point<float> *p = new Point<float>;

Only the float instance (a) point template, (b) new operator, (c) default constructor need to be "present".

Template's Bug report * * *

All types-related tests, if they involve template parameters, must be deferred until the actual operation occurs.

For the following template declaration:

template <class T>
class Mumble
{
public:
Mumble(T t = 1024) : _t(t)
{
if(tt != t)
throw ex ex;
}
private:
T tt;
}

Which is like "t t = 1024", "TT!" A potential error such as "T" is not reported when template declaration, but is checked and recorded at the time of the occurrence of each active operation, and the result will vary depending on the actual type.

Mumble<int> mi;  // 上述两个潜在错 误都不存在
Mumble<int*> pmi;  // 由于不能将一个非零的整数常量指定给一个指针,故 “T t = 1024”错误

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.