Auto_PTR_REF and Auto_PTR relationship
This is an article on the C newsletter. It explains the relationship between Auto_PTR_REF and AUTO_PTR.
You can find "Auto_PTR_REF - Anyone Know What IT's About?" On Google.
This article has a delete.
1 Q:
For the ANSI / ISO C standard, I have two problems, with respect to Auto_Ptr's help auto_ptr_ref.
1) What is the intent of this class.
2) How it works.
2 A:
1)
In the initial proposal, 'auto_ptr''s semantics were broken because of this type of 'Const' object.
The initial proposal is to have a copy constructor with 'const &' as a parameter. However, this will lead to some amazing
Things. ,E.g:
// This is just to reflect the original intention. It is not a standard version.
Void F (std :: auto_ptr
Std :: auto_ptr
// ...
}
Int main () {
Std :: auto_ptr
f (ai);
// OOPS! 'AI' does not have a pointer. This is again, this is not a standard version of the standard.
}
Basically, such rules destroy a copy constructor with 'const &'. Even other people will bring
A worse result. Finally, it is mainly the French delegation they want to "auto_ptr" like this.
- There is no copy constructor with 'autor_ptr &>' as a parameter.
- Allow 'std :: auto_ptr
'std :: auto_ptr
... or, for the C standard, the 'auto_ptr' should be removed from the standard to the French approval ticket, some other
The country has similar ideas to some extent. Therefore, GREG and BILL put forward the idea of 'auto_ptr_ref', so all everything is beautiful. In their first statement, everyone is very surprised to work. But it can, and everything is fine.
2) How do it work?
What it do is that it makes it from a temporary amount (for example, returning from a function) through a group without 'const &'
The number of copy constructor is constructed to construct a 'auto_ptr' object. The problem is, you can't convey a temporary variable
Go to a function with '&' (that is, a very quite reference) as a parameter. which is
Make it not a constructor. And you can't pass value to define a copy constructor because you also need one
Copy constructor. So, how is this complete?
The trick is to convert the definition of two users, which is from 'auto_ptr' to 'auto_ptr_ref' to 'auto_ptr'.
Normal first react is "But there is only one user-defined conversion", which is completely correct and can be
'auto_ptr' is mastered. So, how do you work? , Good, then this processing, two objects are constructed, that is, a result of a temporary amount as a function and a 'auto_ptr'. There is nothing to say about the type of temporary amount.
The trick is: from a temporary amount that is placed on the stack from a 'auto_ptr', it is not a 'auto_ptr'
But an 'auto_ptr_ref' (the compiler is said to be: I look like any other function to any other functions.
An object). This is clear because there is a user-defined from 'auto_ptr' to 'auto_ptr_ref'
Conversion. Now, there is a 'auto_ptr_ref' instead of 'auto_ptr' in the stack. This is very easy
Because there is a 'auto_ptr' constructor, the parameter is an object of 'auto_ptr_ref' type. This is the second
A conversion, but then the user only defines a conversion parameter only. This is all of it. I omitted some
About the ownership of the maintenance pointer is transferred to the details of others, but this only doing some thinker work. we
The part of interest is the two conversions why 'auto_ptr_ref' is to be introduced. GREG and BILL implementation
It's Cool.
// Translator:
// This program is on http://www.c-view.org/tech/pattern/cppptips/auto_ptr_ref, I only
// list it.
#include
Class auto_ptr
{
Private:
Struct auto_ptr_ref {};
PUBLIC:
Auto_PTR () {cout << "auto_ptr :: auto_ptr () / n";
Auto_Ptr (auto_ptr &) {cout << "auto_ptr :: auto_ptr (auto_ptr &) / n";}
Operator auto_ptr_ref ()
{cout << "auto_ptr :: Operator auto_ptr_ref () / n";}
Auto_PTR (auto_ptr_ref) {cout <<
"Auto_PTR :: auto_ptr (auto_ptr_ref) / n";
~ auto_ptr () {cout << "auto_ptr :: ~ auto_ptr () / n";
}
Auto_ptr function1 ();
Auto_ptr function2 ();
int main ()
{
Auto_PTR F1 = Function1 (); // Calls Function Function1 (), See Below Now
// Looks for "auto_ptr :: auto_ptr (const auto_ptr &)", Not found
// can't USE "auto_ptr :: auto_ptr (auto_ptr &)"
// Because Return Value of 'Function1 ()' Is A Temporary
// SO Consider Two-Step Routes Now
// 1) USE "auto_ptr :: operator auto_ptr_ref ()"
// 2) Use "auto_ptr :: auto_ptr (auto_ptr_ref)" to construct 'f1' // call to "auto_ptr :: ~ auto_ptr ()" to destroy return of 'function1 ()'
COUT << "after1 / n";
Auto_PTR F2 = function2 (); // Figure this one Out yourself
COUT << "after 2 / n";
} // 2 Calls to "auto_ptr :: ~ auto_ptr ()" to destroy 'f1' and 'f2'
Auto_Ptr Function1 () // Comments Assume No Return Value Optimization
{
Auto_PTR A; // Call to "auto_ptr :: auto_ptr ()"
Return A; // Call to "auto_ptr :: auto_ptr (auto_ptr &)"
} // Call to "auto_ptr :: ~ auto_ptr ()" to destroy 'a'
Auto_PTR Function2 () // Figure this one out yourself, uses auto_ptr_ref
{
Return auto_ptr ();
}