By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

proper initialization

P: n/a
Given the following definition:

std::vector< std::pair< boost::shared_ptr<Class1>,
boost::shared_ptr<Class2> > >
PairsVector;

What is the best way to insert objects into the vector (without creating
them separately before inserting them)? I tried the following, but the
compiler complains that it cannot find a matching constructor:

PairsVector.push_back( std::pair< boost::shared_ptr<Class1>,

boost::shared_ptr<Class2> >( new Class1( NULL ),

new Class2( NULL ) ) );

On the other hand, this does compile:

PairsVector.push_back( std::pair< boost::shared_ptr<Class1>,

boost::shared_ptr<Class2> >( boost::shared_ptr<Class1>(new Class1( NULL )),

boost::shared_ptr<Class2>(new Class2( NULL )) ) );

Why won't the pair<> constructor accept the raw pointers and simply assign
them to the shared_ptr's?

- Dennis
Apr 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Dennis Jones wrote:
Given the following definition:

std::vector< std::pair< boost::shared_ptr<Class1>,
boost::shared_ptr<Class2> > >
PairsVector;

What is the best way to insert objects into the vector (without
creating them separately before inserting them)? I tried the
following, but the compiler complains that it cannot find a matching
constructor:

PairsVector.push_back( std::pair< boost::shared_ptr<Class1>,

boost::shared_ptr<Class2> >( new Class1( NULL ),

new Class2( NULL ) ) );

On the other hand, this does compile:

PairsVector.push_back( std::pair< boost::shared_ptr<Class1>,

boost::shared_ptr<Class2> >( boost::shared_ptr<Class1>(new Class1(
NULL )),

boost::shared_ptr<Class2>(new Class2( NULL )) ) );

Why won't the pair<> constructor accept the raw pointers and simply
assign them to the shared_ptr's?


Could it be that 'shared_ptr's constructor is _explicit_?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 13 '06 #2

P: n/a
Dennis Jones wrote:
Given the following definition:

std::vector< std::pair< boost::shared_ptr<Class1>,
boost::shared_ptr<Class2> > >
PairsVector;

What is the best way to insert objects into the vector (without
creating them separately before inserting them)? I tried the
following, but the compiler complains that it cannot find a matching
constructor:

PairsVector.push_back( std::pair< boost::shared_ptr<Class1>,

boost::shared_ptr<Class2> >( new Class1( NULL ),

new Class2( NULL ) ) );

On the other hand, this does compile:

PairsVector.push_back( std::pair< boost::shared_ptr<Class1>,

boost::shared_ptr<Class2> >( boost::shared_ptr<Class1>(new Class1(
NULL )),

boost::shared_ptr<Class2>(new Class2( NULL )) ) );

Why won't the pair<> constructor accept the raw pointers and simply
assign them to the shared_ptr's?


Because boost::shared_ptr's constructor is declared as 'explicit' to avoid
the dangers of implicit conversion. Try:

typedef boost::shared_ptr<Class1> tPtr1;
typedef boost::shared_ptr<Class2> tPtr2;

typedef std::pair<tPtr1,tPtr2> tPtrPair;

typedef std::vector< tPtrPair > tPtrPairVector;

tPtrPairVector PtrPairVector;

PtrPairVector.push_back( tPtrPair( tPtr1( new Class1(NULL) )
, tPtr2( new Class2(NULL) )
)
);
If you find yourself doing this a lot, it may be worth adding a
static Ptr function to your Class1 and Class2 classes.

class Class1
{

....

Class1( int aInt ).... // make private

public:

typedef boost::shared_ptr<Class1> tPtr;

static tPtr Ptr( int aInt ){ return tPtr( new Class1( aInt ); }
};

Ditto for Class2,

Then the push_back above is called like:

PtrPairVector.pushBack( tPtrPair( Class1::Ptr(NULL), Class2::Ptr(NULL) ) );

Jeff Flinn

Apr 13 '06 #3

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:e1**********@news.datemas.de...
Why won't the pair<> constructor accept the raw pointers and simply
assign them to the shared_ptr's?


Could it be that 'shared_ptr's constructor is _explicit_?


Duh, of course!!! I know what an explicit constructor is, but I've only
used one in my own classes once before, so it never even entered my mind.
Thank you!

- Dennis
Apr 13 '06 #4

P: n/a
Dennis Jones wrote:

Why won't the pair<> constructor accept the raw pointers and simply assign
them to the shared_ptr's?


Because the shared_ptr constructor that takes a raw pointer is explicit.
It won't be called for an implicit conversion.

--

Pete Becker
Roundhouse Consulting, Ltd.
Apr 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.