O''''Reilly Network For Information About''''s Book part 39 - Pdf 17

never throws.
T* operator->() const;
The operator returns the stored pointer. This, together with operator* is what
makes the smart pointer look like an ordinary pointer. This operator never throws.
T* get() const;
The get function is the preferred way of retrieving the stored pointer when it
might be null (in which case operator* and operator-> leads to undefined
behavior). Note that it is also possible to test whether a shared_ptr contains a
valid pointer by using the implicit Boolean conversion. This function never throws.
bool unique() const;
This function returns true if the shared_ptr is the sole owner of the stored
pointer; otherwise, it returns false. unique never throws.
long use_count() const;
The use_count function returns the reference count for the pointer. It is
especially useful for debugging purposes, because it can be used to get snapshots
of the reference count at critical points of program execution. Use it sparingly. For
some possible implementations of the shared_ptr interface, calculating the
reference count may be expensive or even impossible. The function never throws.
operator unspecified-bool-type() const;
This implicit conversion to a type, unspecified-bool-type, makes it
possible to test a smart pointer in Boolean contexts. The value is TRue if the
shared_ptr is currently storing a valid pointer; otherwise, it is false. Note
that the type that this conversion function returns is not specified. Using bool as
the return type allows for some nonsensical operations, so typically, an
implementation uses the safe bool idiom,
[8]
which is a nifty way of ensuring that
only applicable Boolean tests can be used. The function never throws.
[8]
Invented by Peter Dimov.
void swap(shared_ptr<T>& b);

public:
B(boost::shared_ptr<int> no) : no_(no) {}
int value() const {
return *no_;
}
};
int main() {
boost::shared_ptr<int> temp(new int(14));
A a(temp);
B b(temp);
a.value(28);
assert(b.value()==28);
}
The classes A and B both store a shared_ptr<int>. When creating the
instances of A and B, the shared_ptr temp is passed to their constructors.
This means that all three shared_ptrsa, b, and tempare now referring to the
same instance of an int. Had we used pointers to achieve such sharing of an int,
A and B would have had a hard time figuring out when (and if!) it should be
deleted. In the example, the reference count is 3 until the end of main, where all of
the shared_ptrs go out of scope, decreasing the count until it reaches 0,
allowing the last of the smart pointers to delete the shared int.
The Pimpl Idiom Revisited
The pimpl idiom was previously presented in conjunction with scoped_ptr,
which works well as a means of storing the dynamically allocated instance of the
pimpl, if copying is not permitted for the class using the idiom. That is not
appropriate for all classes that would benefit from using the pimpl idiom (note that
scoped_ptr can still be used, but copy construction and assignment need to be
implemented by hand). For those classes that can handle shared implementation
details, shared_ptr comes into play. When ownership of the pimpl is passed to
a shared_ptr, the copying and assignment operators come for free. You'll recall

protected:
virtual ~A() {};
};
class B : public A {
public:
virtual void sing() {
std::cout << "Do re mi fa so la";
}
};
boost::shared_ptr<A> createA() {
boost::shared_ptr<A> p(new B());
return p;
}
int main() {
typedef std::vector<boost::shared_ptr<A> > container_type;
typedef container_type::iterator iterator;
container_type container;
for (int i=0;i<10;++i) {
container.push_back(createA());
}
std::cout << "The choir is gathered: \n";
iterator end=container.end();
for (iterator it=container.begin();it!=end;++it) {
(*it)->sing();
}
}
The two classes, A and B, contain a single virtual member function sing. B
derives publicly from A, and as you can see, the factory function createA returns
a dynamically allocated instance of B wrapped in a shared_ptr<A>. In main, a
std::vector containing shared_ptr<A> is filled with 10 elements, and


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status