473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

proper initialization

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
4 3383
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
12
by: Sacha Schär | last post by:
Suppose I have an array of Foo's: class Foo { public: int SomeMethode(void); int i; char c; char * p;
1
by: Frank | last post by:
Hi, classTeacher contains a list of classChild ClassChild contains methods, props and so on Most things a classChild can do on its own. But AskPermission cannot be done by the Child, but must be...
2
by: bob | last post by:
Let's say that a class only allows proper initialization through its constructor. Let's also say that you need a global variable of that class, but you can't initialize it at the beginning of the...
12
by: Christoph Zwerschke | last post by:
Usually, you initialize class variables like that: class A: sum = 45 But what is the proper way to initialize class variables if they are the result of some computation or processing as in...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.