SmartPointer | simple Smart Pointer class , including strong_ptr | Transpiler library
kandi X-RAY | SmartPointer Summary
kandi X-RAY | SmartPointer Summary
本實現包含兩种指針: 強指針 strong_ptr 和 弱指針 weak_ptr。基本上可以替換 std::shared_ptr 和 std::weak_ptr. 弱指針對象不負責管理所持有物件的生命周期, 它僅僅維護著一個“弱”引用計數, 並在需要時從自身生成一個強指針. 弱指針的存在是爲了避免因循環引用 (circular references) 而導致智能指針持有的物件無法釋放的情況出現。. 通過專門實現的内存管理器 com_mem_mgr 模版類可以實現對 COM 指針的封裝, 該封裝對 COM 指針自身的引用計數只是在首次持有時增加 1, 在最終釋放時減 1, 中間其餘時間的操作只是通過類 strong_ptr 的引用計數來維護。. 爲了避免我們在使用 strong_ptr 時錯誤地調用了 COM 指針的函數 AddRef 和 Release, 特意提供 _NoAddRefReleaseOnComPtr 類以阻止這種事情的發生。.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of SmartPointer
SmartPointer Key Features
SmartPointer Examples and Code Snippets
Community Discussions
Trending Discussions on SmartPointer
QUESTION
I've overloaded << operator to print the address in pointer member of a class. However, it throws the following error (using Visual Studio 2017). Using a normal class method does the job.
Any leads?
Error:
...ANSWER
Answered 2021-Jun-12 at 15:10Your friend
declares non-template operator.
In this case, you can define a friend function inside of the class body, or try the following:
QUESTION
I am trying to implement some kind of numpy.where() for my ITK images in C++. ITK's way seems to be with Functors. I am not very experienced with templates, so my whole approach might be flawed, but here is my go:
...ANSWER
Answered 2021-May-26 at 13:01You have needlessly many template parameters. You could follow the way it is done in a corresponding test. Define your function, set it via filter->SetFunctor() and call Update()
.
QUESTION
I'm in a case where i use a builder to use factory to create CombatUnit for my project, as show in the schema (please note that I have an abstract factory, but i wouldn't complexify it for nothing).
The thing is, i'm creating arrays of arrays and i'm using unique_ptr to protect from memory leaks. But i can't find if i'm overusing smart_pointer for nothing, here why
...ANSWER
Answered 2021-May-20 at 19:34C++ is Unique_ptr pertinent for vectors of vectors
That's for you to decide, but only rapidly looking at the code without other knowledge of the infrastructure, I would say that this is a heavy, terrible code smell. I would simply use the vector as is. It's lighter, safer, simpler, and faster.
A pointer to a STL containe is usually a strong indicator of code smell, and a pointer to a STL container that contains pointer to STL containers has even a stronger code smell. This would simply not pass review and I would urge to simplify.
Owning pointers to STL containers should practically not exist in a code, unless there is some class that encapsulate very special memory management through container pointers, but they should not escape that class. And even then, you can manage memory and reuse with simple container values.
You should give value semantics a try! It helps local thinking and usually make code simpler.
Now for the non opinion based part of the question.
Let's drop the superfluous unique pointers in your code so it's easier to explain:
QUESTION
I am trying to learn how to use gtkmm having got a basic grasp of C++ (I like a challenge!). I have been working my way through the tutorials (as well as other reading). I am trying to use the approach of using glade to design the UI and then write the code to do the work.
So I have built a very simple UI (window and button at the moment!). I am using the GTK::Builder to load the UI from file. I am dividing the code into classes and a main caller.
Here is the main.cpp
...ANSWER
Answered 2021-Feb-10 at 23:09First of all, to understand the difference, and since you are new to C++, I would recommend reading on the following topics:
First approach:get_widget
With this approach, you are getting a raw pointer (as opposed to a smart pointer) to a widget defined from the ui file. Example:
QUESTION
I'm asked to write a SmartPointer
class. One of the constructors takes a pointer variable, and I assume that I should simply copy the pointer to the relevant variable. But when I try, I get a segmentation error. Here is the content of the header file and my implementation of Pointer Constructor
.
ANSWER
Answered 2021-Feb-09 at 21:25You appear to be writing something similar to shared_ptr
.
For a shared_ptr
-like smart pointer, each smart pointer has both a pointer-to-object and a pointer-to-control-block.
When you are constructed with a pointer-to-object, you are responsible to create the control block.
In your case, your control block name is ReferenceCount
.
So add a new ReferenceCount
to that constructor. Probably start it off with a count of 1.
QUESTION
I am trying to implement an own SmartPointer class and after an initial working version I have started to refine the code and now facing an issue, that I am unable to resolve.
Here is the first version:
...ANSWER
Answered 2021-Jan-08 at 21:25I suppose your problem (with a single constructor) is caused from "vexing parse" (here a description of the "most vexing parse problem", a more spectacular version of the problem).
I mean... if you write
QUESTION
I have a trait classes which are about to be used in variadic CRTP to extend the features of SmartPointer class.
This question is created as the followup of https://stackoverflow.com/a/65373058/5677080
An example trait class:
...ANSWER
Answered 2020-Dec-19 at 22:36Something along these lines, perhaps:
QUESTION
I am about to design and implement a kind of smart pointer toolkit - a set of classes to define various types of smart pointers like unique_ptr, intrusive_ptr, shared_ptr, observing_ptr, tagged_ptr etc. Just to mention I am working in freestanding environment where I have no c++ library available. My intersion is to avoid code duplications, make it follow an elegant design principle. Let me describe my thoughts in there.
Design Considerations: I wanna use variadic CRTP approach to mixin the desired pointer features, the traits. For every feature set there shall be once trait class like:
...ANSWER
Answered 2020-Dec-19 at 18:20You might use variadic template:
QUESTION
Create a class template
SmartPointer
that should contain a pointer to any object and delete that same object when the destructor of that class is called. In order for the smart pointer to behave the same way the raw pointer behaves, you must overlap the operator*
and->
.
So, this is my task, and I've done this code, but the ->
operator is not okay. If someone knows how to fix it, please help me.
ANSWER
Answered 2020-May-19 at 03:25The operator->
function should return a pointer. And it should be a pointer to the wrapped object:
QUESTION
I am testing smart-pointers in Delphi 10.3 Rio using Spring4D. Here is my test program. I created a generic TObjectList
and I want to add simple TObject
s to this list using Shared.Make(TTestObj.Create)
. The problem is that whenever I add an object to the List, the previous object is released. See the output of my program. Does anyone know how to solve this problem?
ANSWER
Answered 2020-May-08 at 19:42The problem is that your TObjectList
holds raw TTestObj
object pointers, not the IShared
interfaces that Shared.Make()
returns.
In var lTestList := Shared.Make(TTestList.Create)();
, you are creating an IShared
(a reference to function: TTestList
) that wraps a TTestList
object you are creating. You are invoking ()
on the IShared
, which calls the function to return the raw TTestList
object pointer. Which is OK in this example, because the IShared
will be held in a hidden variable for the lifetime of Test_SmartPointer()
, thus its refcount is 1, keeping the TTestList
alive.
In var lTestObj := Shared.Make(TTestObj.Create)();
you are doing the same thing, this time for an IShared
returning an TTestObj
object pointer. However, when lTestObj
goes out of scope at the end of each loop iteration, the refcount of the IShared
is decremented. Since there are no further references to that interface, its refcount falls to 0, destroying the object behind the IShared
, which in turn destroys its associated TTestObj
object, leaving the TObjectList
with a dangling TTestObj
pointer (but you don't experience any crashes with that, since you are not accessing the stored TTestObj
objects in any way, not even in the TObjectList
destructor due to OwnsObjects=false
).
You need to change TTestList
to hold IShared
elements instead of TTestObj
elements (in that case, you should use TList
instead of TObjectList
), and get rid of the ()
invocations on the IShared
interfaces when calling Shared.Make()
:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install SmartPointer
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page