proper initialization

D

Dennis Jones

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
 
V

Victor Bazarov

Dennis said:
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
 
J

Jeff Flinn

Dennis said:
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
 
D

Dennis Jones

Victor Bazarov said:
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
 
P

Pete Becker

Dennis said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,538
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top