smart-pointers

Design of (shared_ptr + weak_ptr) compatible with raw pointers

不羁的心 提交于 2019-12-25 08:39:45
问题 Preamble In C++11 there is std::shared_ptr + std::weak_ptr combo. Despite being very useful, it has a nasty issue: you cannot easily construct shared_ptr from a raw pointer. As a result of this flaw, such smart pointers usually become "viral": people start to completely avoid raw pointers and references, and use exclusively shared_ptr and weak_ptr smart pointers all over the code. Because there is no way to pass a raw reference into a function expecting a smart pointer. On the other hand,

Smart pointer test in a while loop: use the comma operator?

为君一笑 提交于 2019-12-25 04:57:27
问题 I recently saw code like this: // 3rd Party API: (paraphrased) void APIResetIterator(int ID); // reset iterator for call to next() Mogrifier* APINext(int ID); // User must delete pointer returned ... typedef std::unique_ptr<Mogrifier> MogPtr; ... const it listID = 42; APIResetIterator(listID); MogPtr elem; while (elem.reset(APINext(listID)), elem) { // use elem } Is this a good idea? Does it work? I'll add the corresponding for loop for easy reference: for (MogPtr elem(APINext(listID)); elem;

shared_from_this() is called after object pointing by this is destroyed: C++ ASIO

拜拜、爱过 提交于 2019-12-25 03:12:34
问题 I am tryin to develop ASIO Application and have referred Chat-Server When my CServer Object destructs it causes CSerSessionsManager Object to destruct- which holds shared pointer to all active chat sessions. It causes all active CSerCession Objects to destroy as well. See the Definitions class CServer { CServer(asio::io_service& io_service, const std::string serIdentity, std::string IP, const std::string port); ~CServer(); ..... private: mutable tcp::acceptor acceptor_; // only in the

thread_local std::unique_ptr release not calling destructor

北城余情 提交于 2019-12-25 02:44:07
问题 Why isn't the destructor called in this code: #include <iostream> #include <thread> #include <memory> class base { public: base() { std::cout << "base()" << std::endl; } virtual ~base() { std::cout << "~base()" << std::endl; } base(const base&) = delete; base(base&&) = delete; base& operator=(const base&) = delete; base& operator=(base&&) = delete; }; class derived final : public base { public: derived() { std::cout << "derived()" << std::endl; } virtual ~derived() { std::cout << "~derived()"

Ambiguous overload when using many typecasts operator overloads

好久不见. 提交于 2019-12-24 18:33:50
问题 I want to create a wrapperClass for strings. I also want the class to be able to return the address of the wrapperClass and the address of the stored (wrapped) string: void FunctionString(string*); void FunctionWrappedString(wrappedString*); int main(){ wrappedString wrappedStringObject; FunctionString(&wrappedStringObject); FunctionWrappedString(&wrappedStringObject); wrappedString anotherWrappedStringObject; if(wrappedStringObject == anotherWrappedStringObject){ // code } } Here are the

clone_ptr problem, I need to create a copy object using a function of the library instead of new

风格不统一 提交于 2019-12-24 11:26:52
问题 I am a bit new to templates in C++ so forgive me if this question is confusing or stupid, I just have a problem where I want to implement a clone smart pointer so I don't have to create copy constructors for each and every class that uses my underlying XML library that only seems to use object pointers and not smart pointers. The problem is that my traits need to create the new objects using functions from the underlying library and I do not know how I would go about doing that in a template

Issue with operator-> overloaded in VS2010

被刻印的时光 ゝ 提交于 2019-12-24 09:41:44
问题 I've implemented a small framework in C++ which I use in a course I give at college, to help students implement their homework. One of the most valuable classes of that framework, is a smart pointer class, which as you can imagine, it overloads the -> operator. Recently I upgraded from VS2008 to VS2010, and occasionally I get issues with the intellisense after typing the operator. Instead of showing the methods and fields available in the pointed data type, it shows the methods and fields of

gtkmm manage/add vs smart pointers:

删除回忆录丶 提交于 2019-12-24 00:14:11
问题 gtkmm provides lifetime management of widgets using this sort of construct: Gtk::Widget* aWidget = Gtk::manage(new Widget()); Gtk::Widget containerWidget; containerWidget.add(*aWidget); This delegates lifetime management of aWidget to containerWidget. When containerWidget is cleaned up, it also cleans up aWidget - similar to Delphi's 'owner' concept. We also have several types of smart pointers, particular the C++ 11 smart pointers templates, which I use everywhere. I find the manage/add

New to c++11 features, proper use of shared_ptr?

﹥>﹥吖頭↗ 提交于 2019-12-23 18:08:48
问题 So my understanding is that a shared_ptr automatically deallocates from memory when the last remaining owner of the object is destroyed or reassigned, (Seems too good to be true?) and it's useful when many instances may be sharing the same object. Correct? So in my case, I'm making a 2d tiled world, so I'm drawing many of the same texture to the screen. I have std::map<int, shared_ptr<Tile>> Tiledb; to store all the tiles. The idea is to only load the texture in once and then i can render it

SmartPointer : cast between base and derived classes

荒凉一梦 提交于 2019-12-23 15:01:26
问题 Say you have a function like this : SmartPtr<A> doSomething(SmartPtr<A> a); And classes like this : class A { } class B : public A { } And now I do this : SmartPtr<A> foo = new B(); doSomething(foo); Now, I would like to get back a SmartPtr<B> object from doSomething . SmartPtr<B> b = doSomething(foo); Is it possible ? What kind of casting do I have to do ? Right now, I just found something I believe ugly : B* b = (B*)doSomething().get() Important notes : I do not have any access to SmartPtr