Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
auto_ptr.cpp
Go to the documentation of this file.
1
29#include <iostream> // for cout, endl
30#include <memory> // for auto_ptr
31
32#include <examples.h>
33
34
35// A simple structure.
36class X
37{
38 int i_;
39
40public:
41
42 X (int i) : i_ (i) {
43 std::cout << "X::X (" << i_ << ')' << std::endl;
44 }
45
46 ~X () {
47 std::cout << "X::~X [" << i_ << ']' << std::endl;
48 }
49
50 int get () const { return i_; }
51};
52
53
54int main ()
55{
56 // a implicitly initialized to 0 (the null pointer)
57 std::auto_ptr<X> a;
58
59 // Establish a scope.
60 if (1) {
61 // b will hold a pointer to an X.
62 std::auto_ptr<X> b (new X (12345));
63
64 // a will now be the owner of the underlying pointer.
65 a = b;
66 }
67
68 std::cout << "b destroyed" << std::endl;
69
70 // Output the value contained by the underlying pointer.
71#ifndef _RWSTD_NO_NONCLASS_ARROW_RETURN
72 std::cout << a->get () << std::endl;
73#else
74 std::cout << (*a).get () << std::endl;
75#endif
76
77 // The pointer will be delete'd when a is destroyed on leaving scope.
78 return 0;
79}
#define i
Definition 17.names.cpp:126
#define i_
Definition 17.names.cpp:180
#define a
Definition 17.names.cpp:109
#define b
Definition 17.names.cpp:110
int main()
Definition auto_ptr.cpp:54
int get() const
Definition auto_ptr.cpp:50
~X()
Definition auto_ptr.cpp:46
X(int i)
Definition auto_ptr.cpp:42