scoped_ptr initialization
Hi All,
I'm new to Boost, and I've been taking a look at scoped pointers. Here's the
example from the boost documentation:
----------------------------------------------------------------------
#include
On Thu, Sep 11, 2008 at 11:24 AM, UberMongoose
Hi All,
I'm new to Boost, and I've been taking a look at scoped pointers. Here's the example from the boost documentation:
---------------------------------------------------------------------- #include
#include <iostream> struct Shoe { ~Shoe() { std::cout << "Buckle my shoe\n"; } };
class MyClass { boost::scoped_ptr<int> ptr; public: MyClass() : ptr(new int) { *ptr = 0; } int add_one() { return ++*ptr; } };
int main() { boost::scoped_ptr<Shoe> x(new Shoe); MyClass my_instance; std::cout << my_instance.add_one() << '\n'; std::cout << my_instance.add_one() << '\n'; }
----------------------------------------------------------------------
I notice that the creation of a new object for the scoped pointer has been done before the MyClass constructor. However you can't declare the scoped pointer as a member variable and then do something like this in a method:
protected: scoped_pointer<ArbitaryObject> mPointerToObject; .... // Later mPointerToObject ( new ArbitaryObject() );
Why is that?
Hi, Quote from the scoped_ptr docs: "The scoped_ptr template is a simple solution for simple needs. It supplies a basic "resource acquisition is initialization" facility, without shared-ownership or transfer-of-ownership semantics." So, you can't transfer ownership - once it's constructed, it points to that object for its lifetime. I use std::auto_ptr or boost::shared_ptr if I want to reassign the pointer during its lifetime. Regards, Pete
I notice that the creation of a new object for the scoped pointer has been done before the MyClass constructor. However you can't declare the scoped pointer as a member variable and then do something like this in a method:
Creation of the object is done in the constructor, though not in the curly brackets.
Why is that?
You can do this: class MyClass { boost::scoped_ptr<int> ptr; public: MyClass() : ptr(new int) { *ptr = 0; } int add_one() { return ++*ptr; } boost::scoped_ptr<int> newObject() { Ptr.reset( new int ); *ptr = 0; } }; ****************************************************************************** "This message and any attachments are solely for the intended recipient and may contain confidential and privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you." Interactive Transaction Solutions Ltd (2473364 England) Registered Office: Systems House, Station Approach Emsworth PO10 7PW ********************************************************************** Ce message �lectronique contient des informations confidentielles � l'usage unique des destinataires indiqu�s, personnes physiques ou morales. Si vous n'�tes pas le destinataire voulu, toute divulgation, copie, ou diffusion ou toute autre utilisation de ces informations, est interdite. Si vous avez re�u ce message �lectronique par erreur, nous vous remercions d'en avertir son exp�diteur imm�diatement par email et de d�truire ce message ainsi que les �l�ments attach�s. Interactive transaction Solutions SAS- France (RCS Pontoise : 489 397 877) Si�ge social : Parc Saint Christophe, 10, Avenue de l�Entreprise 95865 Cergy-Pontoise Cedex ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________
I notice that the creation of a new object for the scoped pointer has been done before the MyClass constructor. However you can't declare the scoped pointer as a member variable and then do something like this in a method:
Creation of the object is done in the constructor, though not in the curly > brackets.
Why is that?
You can do this:
Change that to return a reference to the ptr: boost::scoped_ptr<int>& newObject() { ptr.reset( new int ); *ptr = 0; return ptr; } Or void newObject() { Ptr.reset( new int ); *ptr = 0; } ****************************************************************************** "This message and any attachments are solely for the intended recipient and may contain confidential and privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you." Interactive Transaction Solutions Ltd (2473364 England) Registered Office: Systems House, Station Approach Emsworth PO10 7PW ********************************************************************** Ce message �lectronique contient des informations confidentielles � l'usage unique des destinataires indiqu�s, personnes physiques ou morales. Si vous n'�tes pas le destinataire voulu, toute divulgation, copie, ou diffusion ou toute autre utilisation de ces informations, est interdite. Si vous avez re�u ce message �lectronique par erreur, nous vous remercions d'en avertir son exp�diteur imm�diatement par email et de d�truire ce message ainsi que les �l�ments attach�s. Interactive transaction Solutions SAS- France (RCS Pontoise : 489 397 877) Si�ge social : Parc Saint Christophe, 10, Avenue de l�Entreprise 95865 Cergy-Pontoise Cedex ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________
On Thu, Sep 11, 2008 at 2:18 PM, Patrick Loney
I notice that the creation of a new object for the scoped pointer has been done before the MyClass constructor. However you can't declare the scoped pointer as a member variable and then do something like this in a method:
Creation of the object is done in the constructor, though not in the curly > brackets.
Why is that?
You can do this:
Change that to return a reference to the ptr:
boost::scoped_ptr<int>& newObject() { ptr.reset( new int ); *ptr = 0; return ptr; }
Or
void newObject() { Ptr.reset( new int ); *ptr = 0; }
I never realised scoped_ptr had a reset() method! I knew auto_ptr did. UberMongoose - ignore my earlier reply.
participants (3)
-
Patrick Loney
-
Peter Barker
-
UberMongoose