Array of smart pointers to base class
Here is what I desire to have in my GUI library as it pertains to boost: 1. No raw widgt pointers. Container functions should take a reference to a base class smart pointer type rather then accepting raw pointers of the base class type. 2. Containers store an array of smart pointers of the base GUI widget type. This is needed because containers can contain many widget derived types. I'm just showing the class code that matters; Normally, you would have constructors, destructors, access label, etc. class OWidget; class ODerivedWidget : public OWidget; class OWidgetContainer : public OWidget { public: void Add( boost::shared_ptr<OWidget>& widget ) { mWidgets.pushback(widget); } std::vector<boost::shared_ptr<OWidget>> mWidgets; } int main(void) { OWidgetContainer root; // Works just fine since the types match boost::shared_ptr<OWidget> base(new OWidget()); root.Add(base); // Doesn't work since the Add() function can't convert ODerivedWidget shared pointer to OWidget shared pointer type. boost::shared_ptr<ODerivedWidget> derived(new ODerivedWidget()); root.Add( derived); } Is this even possible? I've read about boost::any and boost:variant and while that might solve my problem of storing smart pointers of differing types, it doesn't allow me to pass smart pointers of derived types. Any ideas/suggestions would be GREATLY appreciated! -scottv
scott velasquez wrote:
Here is what I desire to have in my GUI library as it pertains to boost:
1. No raw widgt pointers. Container functions should take a reference to a base class smart pointer type rather then accepting raw pointers of the base class type. 2. Containers store an array of smart pointers of the base GUI widget type. This is needed because containers can contain many widget derived types.
I'm just showing the class code that matters; Normally, you would have constructors, destructors, access label, etc.
class OWidget;
class ODerivedWidget : public OWidget;
class OWidgetContainer : public OWidget { public:
void Add( boost::shared_ptr<OWidget>& widget ) { mWidgets.pushback(widget); }
std::vector<boost::shared_ptr<OWidget>> mWidgets; }
int main(void) { OWidgetContainer root;
// Works just fine since the types match boost::shared_ptr<OWidget> base(new OWidget()); root.Add(base);
// Doesn't work since the Add() function can't convert ODerivedWidget shared pointer to OWidget shared pointer type. boost::shared_ptr<ODerivedWidget> derived(new ODerivedWidget()); root.Add( derived); }
Is this even possible? I've read about boost::any and boost:variant and while that might solve my problem of storing smart pointers of differing types, it doesn't allow me to pass smart pointers of derived types.
Any ideas/suggestions would be GREATLY appreciated!
-scottv
hi scott, make your Add() function take const references and it works. void Add( const boost::shared_ptr<OWidget>& widget ) -- HTH dave
"David Klein" <dave_chp@gmx.de> wrote in message news:451D3699.3040703@gmx.de...
hi scott,
make your Add() function take const references and it works.
void Add( const boost::shared_ptr<OWidget>& widget )
--
HTH dave
That worked flawlessly! You have no idea how happy this makes me ;) Seriously though, I was at the point where I was considering that maybe boost wasn't for me. Thanks for the quick replies! -scottv
Maybe I'm missing something, but how much do you really save by passing a shared pointer by reference? Isn't passing a shared pointer by reference kind of dangerous? Can you _always_ be guaranteed that it will work ok and the ref count won't go to zero and cause the object to be destroyed? I've always followed the guideline of passing shared pointers by value. Copying a shared pointer is extremely fast, or am I missing something? Phillip Hellewell
Hi, Noob question, how do we rename a file obtained from a directory_iterator. i'm attempting to compile the following much simplified example. directory_iterator i("whateverPath"); i++; // i know i++ exists rename (i->current_path(), TestRename); When i try to compile the above , it tells me that i is actually a "path" object not a directory iterator. However, when i try to write something like path test=i; it tells me i is actually a directory iterator not a path object. I know I'm, probably doing something really stupid but would appreactiate some help nonetheless. Thank you
Andrei Danaila wrote:
Hi,
Noob question, how do we rename a file obtained from a directory_iterator. i'm attempting to compile the following much simplified example.
directory_iterator i("whateverPath"); i++; // i know i++ exists rename (i->current_path(), TestRename);
When i try to compile the above , it tells me that i is actually a "path" object not a directory iterator.
i->current_path() means (*i).current_path(). Maybe you need i.current_path()?
On 9/29/06, Andrei Danaila <adanaila_ca@hotmail.com> wrote:
Noob question, how do we rename a file obtained from a directory_iterator. i'm attempting to compile the following much simplified example.
directory_iterator i("whateverPath"); i++; // i know i++ exists rename (i->current_path(), TestRename);
When i try to compile the above , it tells me that i is actually a "path" object not a directory iterator.
That is because you are dereferencing the iterator to the underlying path object. The compiler is correct. Try: rename (i.current_path(), TestRename); // dot not arrow!
However, when i try to write something like
path test=i;
it tells me i is actually a directory iterator not a path object.
The compiler is again correct. Try: path test=*i; // dereference the iterator to get the path HTH, --Mike
Really sorry to be a pest, thanks alot for all the replies, I'm making progress. what i am trying now is: path test("Test2") string randomString="Random"; directory_iterator dir_itr(test); rename(*dir_itr, randomString); It compiles fine but i get a runtime error, I've used exception handling and i get the following message: "Assertion failed: m_imp.get(), file c:\boost2\libs\filesystem\build\../src/operations_posix_windows.cpp, line 317"
From: "Michael Fawcett" <michael.fawcett@gmail.com> Reply-To: boost-users@lists.boost.org To: boost-users@lists.boost.org Subject: Re: [Boost-users] [FileSystem]Directory Iterator Question Date: Fri, 29 Sep 2006 12:13:54 -0400 MIME-Version: 1.0 Received: from wowbagger.osl.iu.edu ([129.79.245.252]) by bay0-mc12-f15.bay0.hotmail.com with Microsoft SMTPSVC(6.0.3790.2444); Fri, 29 Sep 2006 09:14:11 -0700 Received: from wowbagger.osl.iu.edu (localhost [127.0.0.1])by wowbagger.osl.iu.edu (Postfix) with ESMTP id AF92C2C29D;Fri, 29 Sep 2006 12:14:01 -0400 (EDT) Received: from wr-out-0506.google.com (wr-out-0506.google.com [64.233.184.238])by wowbagger.osl.iu.edu (Postfix) with ESMTP id B47F52C092for <boost-users@lists.boost.org>; Fri, 29 Sep 2006 12:13:55 -0400 (EDT) Received: by wr-out-0506.google.com with SMTP id 57so323735wrifor <boost-users@lists.boost.org>; Fri, 29 Sep 2006 09:13:55 -0700 (PDT) Received: by 10.90.52.2 with SMTP id z2mr1701527agz;Fri, 29 Sep 2006 09:13:55 -0700 (PDT) Received: by 10.90.79.20 with HTTP; Fri, 29 Sep 2006 09:13:54 -0700 (PDT) X-Message-Info: txF49lGdW42U7tKBcVJBUOp915aBLRzX/R6GCQUqRgc= X-Original-To: boost-users@lists.boost.org Delivered-To: boost-users@lists.boost.org DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com;h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references;b=E9CcnLr/OYsSmONqDaDUCnfLYVPF4p6Sms9nTHC2F60XK+CjM1twaWlUoUf2xqc8UJ6RFGfEMKK5dolZLyZ31VKs+phEEAvfVWI9KvajagKNfaUJky5K7IeWJXBHVvGzDW+24MgBVbLqsEvM58BPYCq2BEOeOvkKVhB6qZM+6kA= References: <002601c6e3db$cdcee870$6707a8c0@pdimov2><BAY122-F9E5A05F33E0A7BCD451399D180@phx.gbl> X-BeenThere: boost-users@lists.boost.org X-Mailman-Version: 2.1.6 Precedence: list List-Id: Boost Users mailing list <boost-users.lists.boost.org> List-Unsubscribe: <http://lists.boost.org/mailman/listinfo.cgi/boost-users>,<mailto:boost-users-request@lists.boost.org?subject=unsubscribe> List-Archive: <http://lists.boost.org/MailArchives/boost-users> List-Post: <mailto:boost-users@lists.boost.org> List-Help: <mailto:boost-users-request@lists.boost.org?subject=help> List-Subscribe: <http://lists.boost.org/mailman/listinfo.cgi/boost-users>,<mailto:boost-users-request@lists.boost.org?subject=subscribe> Errors-To: boost-users-bounces@lists.boost.org Return-Path: boost-users-bounces@lists.boost.org X-OriginalArrivalTime: 29 Sep 2006 16:14:11.0441 (UTC) FILETIME=[4CA19610:01C6E3E2]
On 9/29/06, Andrei Danaila <adanaila_ca@hotmail.com> wrote:
Noob question, how do we rename a file obtained from a
directory_iterator.
i'm attempting to compile the following much simplified example.
directory_iterator i("whateverPath"); i++; // i know i++ exists rename (i->current_path(), TestRename);
When i try to compile the above , it tells me that i is actually a "path" object not a directory iterator.
That is because you are dereferencing the iterator to the underlying path object. The compiler is correct. Try:
rename (i.current_path(), TestRename); // dot not arrow!
However, when i try to write something like
path test=i;
it tells me i is actually a directory iterator not a path object.
The compiler is again correct. Try:
path test=*i; // dereference the iterator to get the path
HTH,
--Mike _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (6)
-
Andrei Danaila
-
David Klein
-
Michael Fawcett
-
Peter Dimov
-
Phillip Hellewell
-
scott velasquez