|
Boost Users : |
From: Michael Nicolella (boost_at_[hidden])
Date: 2006-07-13 22:19:14
What's wrong with
boost::shared_ptr<Test> test( new Test );
boost::shared_ptr<Test> test( new Test( 100, 'a', "hello" ) );
http://boost.org/libs/smart_ptr/shared_ptr.htm
I'm not sure how NEW0 or NEW or New is better than new... Making a macro
that calls new doesn't encapsulate the call to new, it only obscures it and
makes it less flexible.
-----Original Message-----
From: bringiton bringiton [mailto:kneeride_at_[hidden]]
Sent: Thursday, July 13, 2006 6:38 PM
To: boost-users_at_[hidden]
Subject: [Boost-users] Macro to contruct/allocate a shared_ptr?
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.
// not sure how to do this (or whether possible) // i am not sure how to
implement the variable parameters // (hense the .. in my syntax) template
<class T> boost::shared_ptr<T> New(...) {
boost::shared_ptr<T> p(new T(...));
return p;
}
Any ideas? I often have problems with the c++ syntax when creating my own
containers because i want to allocate T within the implementation (instead
of passing pointers and having the user manage the memory).
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
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