|
Boost Users : |
From: David Abrahams (dave_at_[hidden])
Date: 2006-07-15 10:42:56
"bringiton bringiton" <kneeride_at_[hidden]> writes:
> I'm trying to define a MACRO that creates a shared_ptr and hides
> memory allocation. Here is what i have so far:
>
> #define NEW0(T) boost::shared_ptr<T>(new T());
> #define NEW1(T, p1) boost::shared_ptr<T>(new T(p1));
> #define NEW2(T, p1, p2) boost::shared_ptr<T>(new T(p1, p2));
> #define NEW3(T, p1, p2, p3) boost::shared_ptr<T>(new T(p1, p2, p3));
>
> ---------------------------------
> Example of usage for the type Test:
>
> class Test {
> public:
> Test() {;}
> Test(int p1) {;}
> Test(int p1, char p2) {;}
> Test(int p1, char p2, std::string p3) {;}
> };
>
> boost::shared_ptr<Test> p0 = NEW0(Test);
> boost::shared_ptr<Test> p1 = NEW1(Test, 100);
> boost::shared_ptr<Test> p2 = NEW2(Test, 100, 'a');
> boost::shared_ptr<Test> p3 = NEW3(Test, 100, 'a', "hello");
>
> ---------------------------------
> Is there a better way to do this? I would prefer to use a function
> instead of a MACRO. But I don't think it is possible because it is not
> know how many paramaters are in T's constructor.
The basic intention is sound, but there are better ways to do it, that
don't involve macros (for the user):
http://article.gmane.org/gmane.comp.lib.boost.devel/140159
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net