.net - c++/cli ref class and dynamic allocation using handles? -


in .net's version of c++, can declare class in following way:

ref class coolclass {   public:       string^ getname()       { return name;}        void setname(string^ n)       {name = n;}    private:       string^ name;  } 

when create class way, instances of class created , managed on managed heap clr's garbage collector.

now, lets created 2 instances of animal class.

animal cat; animal ^dog = gcnew animal(); 

both of these classes operate same. there real important difference between creating instances of classes 1 way or other? both should managed code right? first way seems easier , prevents me having use "->" operator.

the first syntax called stack semantics. simulates stack allocation in standard c++. upon leaving scope, including when exception raised, object automatically disposed. nice syntactic convenience, under hood both objects instantiated on managed heap. use first syntax disposable types only, such database connections.

as differences, have use % unary operator convert instance declared stack semantics underlying handle type if must pass method expects handle type.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -