简体   繁体   中英

Classes and Instances in C++/CLI

I have been using C#.net for a very long time, recently I decided that it would be beneficial to switch to C++/CLI. So, I decided to get a converter and convert one of my larger projects to C++/CLI, I found a pretty good converter, there was just a few BIG problems:

For every non-static class that I had in my C# project, the compiler came up with an error whenever the code was making a new instance of it. "Missing type specifier - int assumed..." All of the classes are titled public ref class ClassName : BaseClassName This is also very confusing because the classes that were supposed to be static and the classes that weren't supposed to be static are both titled like this.

The converter also left comments in places in the code that say this: //C++ does not allow initialization of non-static fields in their declarations: these are always in one of my original non static classes and are always followed by Type VariableName or Type ^VaribleName

So since obviously I don't understand how C++/CLI classes work, my question is: What are the differences in dynamic classes in C# and C++/CLI? How do I make a dynamic C++/CLI class and what could I be doing wrong? Why are all of the classes, weather static or not in C#, prefixed with "ref" in C++/CLI? Also, by any chance, does this have anything to do with having multiple classes in one file?

If you need more information please tell me.

You'll need to understand C++/CLI in order to really make sense of the code the converter creates.

Basically, in C++/CLI, ref class is a C# style class. A normal class (without ref ) is a native C++ class definition.

When converting from C#, all C# class declarations will end up as ref class since they are a managed (.NET) class.

The comments of //C++ does not allow initialization of non-static fields in their declarations are because you can't have a class which defines its member inline in C++/CLI. For example, this C#:

class Foo
{
    private int test = 3;
}

Would need to be translated to C++/CLI using a constructor:

ref class Foo
{
private:
    int test;
public:
    Foo()
    {
       test = 3;
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM