Can I use my own primitive types in C ++ / CLI?

Same as C #, int is an alias for System :: Int32, which is a CLR type. Is there a way that I can use my own "int", which means int in standard C ++?

+4
source share
3 answers

C ++ / CLI primitive types have a dual identity. Keywords correspond to both the native C ++ type and the CLR type, depending on the context. Variables with a native type type inherit methods available in the CLR type, for example, ToString .

I think this blog article explaining the relationship between native and managed types is very useful: http://blogs.msdn.com/b/branbray/archive/2005/07/20/441099.aspx

+2
source

I think the accepted answer does not answer the OP question.

If the compiler switch /clr on, then the int keyword means System::Int32 , it is not native.
If the compiler /clr switch is off, the int keyword means native C / C ++ int , it is not controlled.

Using System::Int32 implies a significant decrease in performance over native int (you can measure it).

You can disable the /clr compiler for the .cpp file. This is not a project option, it is a translation option (.cpp file).
Right-click the file, then properties, etc.

So you can write your own code in a .cpp file (compiled with /clr off).
Then you can write managed code in another file (compiled with /clr on) and call your own code.

+1
source

They are the same thing, go and use int wherever you want your own type.

-1
source

All Articles