Wednesday, August 4, 2010

C + + Monitor: compatible with the accepted type of member function templates

smart pointers (smart pointer) is behaves like a pointer but the pointer does not provide increased functionality of the objects. For example, "C + + Monitor: Using Object Management Resources" set the standard auto_ptr and tr1:: shared_ptr what is applied at the right time automatically remove the heap-based resources (heap-based resources). STL containers within the iterators (iterators) is almost always smart pointers (smart pointer); you definitely can not expect to use "+ +" will be a built-in pointer (built-in pointer) from a linked list (linear list ) of a node to the next, but the list:: iterators can do it.

real pointers (the real pointer) did a very good thing to support implicit conversions (implicit conversion). derived class pointers (derived class pointer) implicitly converted to a base class pointers (base class pointer), pointers to non-const objects (object pointers points to a very content) converted to pointers to const objects (pointing to the constant object pointer), etc. and so on. For example, consider a three-level hierarchy (three inheritance system) can occur in a number of conversion: class Top (...);
class Middle: public Top (...);
class Bottom: public Middle (...);
Top * pt1 = new Middle; / / convert Middle * => Top *
Top * pt2 = new Bottom; / / convert Bottom * => Top *
const Top * pct2 = pt1; / / convert Top * => const Top *
In the user-defined smart pointer classes (user-defined smart pointer class) in imitation of the conversion is tricky. We want to compile the following code: template
class SmartPtr (
public: / / smart pointers are typically
explicit SmartPtr (T * realPtr); / / initialized by built-in pointers

SmartPtr pt1 = / / convert SmartPtr =>
SmartPtr (new Middle); / / SmartPtr

SmartPtr pt2 = / / convert SmartPtr =>
SmartPtr (new Bottom); / / SmartPtr

SmartPtr pct2 = pt1; / / convert SmartPtr =>
/ / SmartPtr
In the same template (template) of the different instantiations (instantiated) have no inherent relationship (inheritance), so the compiler that the SmartPtr and the SmartPtr is completely different classes, no less than (say) vector and closer to Widget . In order to get what we want the transition between the SmartPtr classes, we need to explicitly program them.

In the above smart pointer (smart pointer) of the sample code, every statement to create a new smart pointer object (smart pointer object), so now we focus on how we write smart pointer constructors (smart pointer constructor), the way we want it to run. A key fact is that we can not write that we need all the constructors (constructor). In the above hierarchy (inheritance system), we can construct a SmartPtr or a SmartPtr a SmartPtr, but if the future of this hierarchy (inheritance system) to be expanded, SmartPtr objects must also be from other smart pointer types (smart pointer type) constructed. For example, if we later joined the class BelowBottom: public Bottom (...);
We need support from SmartPtr objects to SmartPtr objects of creation, and we certainly do not want to do this and must be changed SmartPtr template.

In general, we need constructors (constructor) the number is unlimited. As a template (template) can be caused by numerous examples of functions, so if we do not need to SmartPtr a constructor function (the constructor function), we need a constructor template (template constructor). Such templates (templates) is member function templates (member function template) (often appropriately referred to as member templates (members of the template)) - produce a class of member functions (member functions) of the templates (templates) example: template
class SmartPtr (
template / / member template
SmartPtr (const SmartPtr & other); / / for a "generalized
... / / Copy constructor "
This means that for each type T, and every type of U, can be created from a SmartPtr a SmartPtr, because there is a SmartPtr to get a SmartPtr argument constructor (constructor). Like this constructor (constructor) - from a type is the same template (template) to create different instances of the object to another object's constructor (constructor) (for example, from a SmartPtr to create a SmartPtr) - sometimes known as the generalized copy constructors (generic of copy constructor).

The above generalized copy constructor (generic of copy constructor) has not been declared to be explicit (explicit) in the. This is intentionally. built-in pointer types (built-in pointer type) between the type of conversion (for example, from a derived class pointer to base class pointer) is implicit and do not need to cast (forced transition), so make smart pointers (smart pointer) mimic this behavior is reasonable. In templatized constructor (templated constructor) omit explicit just do it.

As a statement, SmartPtr the generalized copy constructor (generic of copy constructor) to provide something more than than we want. Yes, we need to be able to create from a SmartPtr a SmartPtr, but we do not need to create one from a SmartPtr SmartPtr, it's like reverse public inheritance (public inheritance) meaning (see "C + + maxim: to ensure that public inheritance simulation" is- a ""). We do not need to be able to create from a SmartPtr a SmartPtr, as this and from int * to double * the implicit conversion (implicit conversion) is not commensurate. We must try to filter from this member template (members of the template) generated member functions (member functions) of the group.

If SmartPtr follow auto_ptr and tr1:: shared_ptr's footsteps, to provide a return by the smart pointer (smart pointer) holds the built-in pointer (built-in pointer) copies of the get member function (get member function) (see "C + + Proverbs: in resource management class to prepare to access bare resources "), we can use constructor template (constructor template) to achieve the transformation we want the scope limited to: template
class SmartPtr (
SmartPtr (const SmartPtr & other) / / initialize this held ptr
: HeldPtr (other.get ()) (...) / / with other''s held ptr

T * get () const (return heldPtr;)

private: / / built-in pointer held
T * heldPtr; / / by the SmartPtr
Through member initialization list (member initialization list), held with SmartPtr pointer type U * initialize the type T * SmartPtr's data member (data member). This is only "there is a pointer from a U * T * pointer to an implicit conversion (implicit conversion)" can be compiled under the condition, which is what we want. The ultimate effect is SmartPtr now have a generalized copy constructor (generic of copy constructor), it is only in passing in a compatible type (incompatible types) of parameters can be compiled.

member function templates (member function template) is not limited to the use of constructors (constructor). Another common task they are used to support the assignment (assignment). For example, TR1's shared_ptr (again, see "C + + Monitor: Using Object Management Resources") support from all compatible with the built-in pointers (built-in pointer), tr1:: shared_ptrs, auto_ptrs and tr1:: weak_ptrs structure, as well as from the addition tr1 :: weak_ptrs than all of these assignments. Here is an extract from the TR1 specification out of a paragraph on the tr1:: shared_ptr content, including its statement template parameters (template parameters) to use class instead of typename preferences. (Like "C + + Proverbs: Understanding the two meanings of typename" in the set, in context here, they are strictly the same meaning.) Template class shared_ptr (
template / / construct from
explicit shared_ptr (Y backup bin conf config data eshow_sitemap.html log maint sitemap.html svn tmp p); / / any compatible
template / / built-in pointer,
shared_ptr (shared_ptr const & r); / / shared_ptr,
template / / weak_ptr, or
explicit shared_ptr (weak_ptr const & r); / / auto_ptr
explicit shared_ptr (auto_ptr & r);
template / / assign from
shared_ptr & operator = (shared_ptr const & r); / / any compatible
template / / shared_ptr or
shared_ptr & operator = (auto_ptr & r); / / auto_ptr
In addition to generalized copy constructor (generic of copy constructor), all of these constructors (constructor) are explicit (explicit) in the. This means that from a shared_ptr to another type of implicit conversion (implicit conversion) is permitted, but from a built-in pointer (built-in pointer) or smart pointer type (smart pointer type) implicit conversion (implicit conversion) is not allowed. (Explicit conversion (explicit conversion) - for example, by a cast (forced transition) - is still possible.) Similarly, attention is auto_ptrs transmitted to tr1:: shared_ptr the constructors (constructor) and the assignment operators (assignment operator) approach has not been declared as const, this control is tr1:: shared_ptrs and tr1:: weak_ptrs's been the means of transmission. This is auto_ptrs copied unique be changed when an inevitable result of the fact that (see "C + + Monitor: Using Object Management Resources").

member function templates (member function template) is an excellent thing, but they do not change the basic rules of the language. "C + + motto: Learn C + + secretly added and called that" set the compiler can generate the four member functions (member functions) of which two are copy constructor (copy constructor), and copy assignment operator (copy assignment operator) . tr1:: shared_ptr declare a generalized copy constructor (generic of copy constructor), it is clear that when the same type T and Y, generalized copy constructor (generic of copy constructor) can be instantiated and become " ; normal "copy constructor (" conventional "copy constructor). So, when a tr1:: shared_ptr object from another of the same type tr1:: shared_ptr object constructor, the compiler is tr1:: shared_ptr generate a copy constructor (copy constructor), or an instance of generalized copy constructor template (Pan type of copy constructor template)?

Like I said, member templates (members of the template) does not change the language rules, and rules, if a copy constructor (copy constructor) is required and you do not declare, you will automatically generate a. Declared in a class of a generalized copy constructor (generic of copy constructor) (a member template (members of the template)) does not prevent the compiler to generate their own copy constructor (copy constructor) (non-template), so if You have to govern all aspects of copy construction (copy constructor), you must either declare a generalized copy constructor (generic of copy constructor) has declared a "normal" copy constructor ("conventional" copy constructor). The same applies to assignment (assignment). This is from the tr1:: shared_ptr extract the definition of the section can be used as an example: template class shared_ptr (
shared_ptr (shared_ptr const & r); / / copy constructor

template / / generalized
shared_ptr (shared_ptr const & r); / / copy constructor

shared_ptr & operator = (shared_ptr const & r); / / copy assignment

template / / generalized
shared_ptr & operator = (shared_ptr const & r); / / copy assignment
Things to Remember

* Use of member function templates (member function template) to generate accept all types of functions compatible.

If you are the generalized copy construction (generic of copy-constructor) or generalized assignment (generic of assignment) a statement of the member templates (members of the template), you still need to declare a normal copy constructor (regular copy constructor), and copy assignment operator ( copy assignment operator).

Recommended links:

Top Food And Drink

Teach you a trick: how to BEAUTIFY MM eyebrows, eyelashes and eyes

Review HOBBY

"Bullwhip Effect" Of The Comprehensive Management

COREL KNOCKOUT matting examples Guide (4)

Farewell Babyface, ISee 1 Minute To Create Face-lift Effect

Do not let the knowledge base has become garbage

Ipod Touch Video Format

Warning: "conditions" soft CONSTRAINTS

Mp3 To 3g2

Avi To Mpeg Converter

converter flv to 3gp

Clear player manual history

PIMS And Calendars Report

Order doors: THE most feared Dell

FreeBSD Forget The Root Password Of A Repair

No comments:

Post a Comment