473,799 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

proper initialization

Given the following definition:

std::vector< std::pair< boost::shared_p tr<Class1>,
boost::shared_p tr<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.pus h_back( std::pair< boost::shared_p tr<Class1>,

boost::shared_p tr<Class2> >( new Class1( NULL ),

new Class2( NULL ) ) );

On the other hand, this does compile:

PairsVector.pus h_back( std::pair< boost::shared_p tr<Class1>,

boost::shared_p tr<Class2> >( boost::shared_p tr<Class1>(new Class1( NULL )),

boost::shared_p tr<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 3415
Dennis Jones wrote:
Given the following definition:

std::vector< std::pair< boost::shared_p tr<Class1>,
boost::shared_p tr<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.pus h_back( std::pair< boost::shared_p tr<Class1>,

boost::shared_p tr<Class2> >( new Class1( NULL ),

new Class2( NULL ) ) );

On the other hand, this does compile:

PairsVector.pus h_back( std::pair< boost::shared_p tr<Class1>,

boost::shared_p tr<Class2> >( boost::shared_p tr<Class1>(new Class1(
NULL )),

boost::shared_p tr<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_p tr<Class1>,
boost::shared_p tr<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.pus h_back( std::pair< boost::shared_p tr<Class1>,

boost::shared_p tr<Class2> >( new Class1( NULL ),

new Class2( NULL ) ) );

On the other hand, this does compile:

PairsVector.pus h_back( std::pair< boost::shared_p tr<Class1>,

boost::shared_p tr<Class2> >( boost::shared_p tr<Class1>(new Class1(
NULL )),

boost::shared_p tr<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_p tr's constructor is declared as 'explicit' to avoid
the dangers of implicit conversion. Try:

typedef boost::shared_p tr<Class1> tPtr1;
typedef boost::shared_p tr<Class2> tPtr2;

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

typedef std::vector< tPtrPair > tPtrPairVector;

tPtrPairVector PtrPairVector;

PtrPairVector.p ush_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_p tr<Class1> tPtr;

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

Ditto for Class2,

Then the push_back above is called like:

PtrPairVector.p ushBack( tPtrPair( Class1::Ptr(NUL L), Class2::Ptr(NUL L) ) );

Jeff Flinn

Apr 13 '06 #3

"Victor Bazarov" <v.********@com Acast.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
4203
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
12
1936
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
1058
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 done by the teacher. Child has to ask its teacher to do that. Is it a proper OO way to have a reference to the teacher to call a teachermethod? Thanks
2
347
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 program. What is the most practical solution?
12
7182
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 the following silly example (representative for more: class A:
8
3126
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 initialized before the program starts executing." -- What does this mean? What is the name of the stage in which the mentioned initialization is performed? Compile-time or run-time? In the following snippet, variables b and c are defined at line 7...
23
3664
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 the built-in types, the value is usually garbage. Is this right? However, I'm a bit confused about value-initialization, when does it happen, and what kind of values are assigned to objects?
4
3714
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 value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
20
6102
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.) main(). So, if the above is OK, does static initialization occur during A or B? What happens during A?
11
2411
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 happens here Suppose we have a function void fn(Test arg);
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.