您可以使用某种句柄来参数,即:
#include <cstdio>
#include <boost/math/distributions/uniform.hpp>
template< class T>
class Handle {
T* rep_;
int* pcount_;
public:
T* operator->() { return rep_;}
Handle( T* rep) : rep_( rep), pcount_( new int(1)) {}
Handle( const Handle& r) : rep_( r.rep_), pcount_( r.pcount_) {
(*pcount_)++;
}
Handle& operator=( const Handle* r) {
if ( rep_ == r->rep_) return *this;
if ( --(*pcount_) == 0) {
delete rep_;
delete pcount_;
}
rep_ = r.rep_;
pcount_ = r.pcount_;
(*pcount_)++;
return *this;
}
~Handle() {
if ( --(*pcount_) == 0) {
delete rep_;
delete pcount_;
printf( "~Handle()");
}
}
};
这样的句柄可以自由传递,它们都可以共享单一的表示:
void f1( Handle<boost::math::uniform_distribution<int> > b) {
Handle<boost::math::uniform_distribution<int> > a = b;
}
Handle<boost::math::uniform_distribution<int> > f2() {
Handle<boost::math::uniform_distribution<int> > h(
new boost::math::uniform_distribution<int>);
return h;
}
/*
*
*/
int main(int argc, char** argv) {
Handle<boost::math::uniform_distribution<int> > h = f2();
f1( h);
Handle<boost::math::uniform_distribution<int> > k = h;
return 0;
}
〜句柄()
运行成功(总时间:62ms)