473,809 Members | 2,733 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 3417
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
4205
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
1059
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
3127
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
3671
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
3718
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
6104
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
2413
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
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
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
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
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
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6881
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
5550
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...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
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.