Boost logo

Boost Users :

From: bringiton bringiton (kneeride_at_[hidden])
Date: 2006-07-14 00:41:45


On 7/14/06, Michael Nicolella <boost_at_[hidden]> wrote:
> 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 mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

I just have a problem with having the user manage the memory. ie I'd
rather do the following:

shared_ptr<Test> p(1, 'a', "hello");
or:
shared_ptr<Test> p = newptr<Test>(1, 'a', "hello");

instead of:

shared_ptr<Test> p(new p(1, 'a', "hello"));

but i guess that's a limitation of the language. to get around this i
do the following:

class Test {
private:
   Test(int p1, char p2, string p3) {;}
public:
   static shared_ptr<Test> NewTest(int p1, char p2, string p3) {
      return p(new Test(p1, p2, p3);
   }
...

// memory allocation hidden
shared_ptr<Test> t = Test::NewTest(1, 'a', "hello");

but this is a hassle. esp when many constructors...


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