I just came across a C ++ SDK that heavily uses this really weird pattern *new. I donβt understand why they do this.
*new
What is the point of building objects using * new, for example. Widget wag = *new Widget();?
Widget wag = *new Widget();
Update: I wonder what they are actually doing XPtr<T> p = *new T;- should be the semantics of some kind of smart pointer magic. Still doesn't make much sense. I am really sure that the SDK is of high quality.
XPtr<T> p = *new T;
It creates a new object and then copies it. The pointer to the source object is discarded, so a memory leak may occur.
. , Widget . , .
Widget
. , , , . , , , . . , ?
, ? , , , .
: . , SDK .
: , , , . XPtr, , , . SDK - - , , . , , : , .
:
Widget wag = *(new Widget()); // or Widget wag;
, . , .
, . , , .
commom , ref, , IncRef:
SmartPtr<Type> ptr = new Type();
, IncRef:
SmartPtr<Type> ptr = *new Type();
, IncRef. ref, 2, , .
, , . XPtr, , , .
. ,
int &a = *new int(); delete &a;
. ( ), , . ,
template<typename T> struct XPtr { T *ptr; XPtr(T &t):ptr(&t) { } ~XPtr() { delete ptr; } }; ... XPtr<T> p = *new T;
Or even more complex, with reference counting - so that p can be copied, and XPtr keeps track of all its copies, etc.
Source: https://habr.com/ru/post/1702487/More articles:Error accessing RSS feed from Silverlight using WebClient () - silverlightBest way to implement MySQL hash table? - mysqlDefining Grails Domain ID in Bootstrap.groovy - grailsDatabase Performance and Data Types - sqlHow to write binary data for 7z archive format? - c ++How can I make friendly URLs for my site? - apacheHow to install MonoDevelop on my Mac? - monodevelopError opening serial port - .netWhere to use using statements in a C # .cs file - c #Matching sqlserver means that the column names must be correct. And how to deal with it - sql-serverAll Articles