Changes in C ++ in Visual Studio 2005

zhaozj2021-02-16  49

Changes in C in Visual Studio 2005

Next generation Visual Studio version (former WHIDBEY, now Visual Studio 2005) offers a richer improvement library file and pressure without many behind-the-scenes, which will make developers' development process. It became simple and interesting. For me, these are pale compared to Visual Studio 2005 to C 's handling, here I mainly talk about C C in the next generation of Visual Studio.

Follow the next line

The processing of the C extension is introduced in Visual Studio.NET. The extension is keywords starting with two underscores, such as __gc and __property. Since the last version released, I have written a large number of double downline characteristics. The code, I don't like this, I understand the specific reason is: The keyword with two underscores as a special extension, which will not be confused with the standard compiler, you can use other other Compiler to compile, it ignores __ keywords.

This is a solution: Microsoft discovered a method without changing the changes in programming language, but there will be some results below:

· Developers find grammar unnatural

· Can't be completely adopted

As example, an appropriate C processing method:

PUBLIC __GC CLASS FOO

{

// Hundreds of Lines of code

__property string * get_text ();

// Hundreds of Lines of code

__Property Void Set_Text (String *);

// Hundreds of Lines of code

}

The programmer with good habits put GET and PUT on the right side of each extension, put the potential variable next to, but the programming language did not ask you. It provides an environmentally supported dominant structure that makes you understand that it is correct as a unit, so it is unnatural with other Visual Studio .NET languages. But how do you deal with this problem, the only way to complete C convert to CLR, if you want to do it, you can have enough freedom to get natural first-class language will give you the best, you can cancel A large number of keywords with double scribes.

Survival time and range

I like to be determined, in fact, I also like fragments, I can also mention more, they have their own role and I also need them, if I only construct an object in memory, I hope to clear it myself. It is not cleared before it.

So memory management is very complicated. However, if your object contains a non-managed source file such as database link, an open file, or like I have to get a control. I want to know that it will leave as soon as possible, use this mode to deal with, but it is not directly, simple intimate support is the best way.

Here is in the original C , you will complete these things:

// this is a code fragment

{

Try

{

Foo * f = new foo (/ * params * /);

// All Kinds of Code, Some of Which Might Throw Exceptions

delete f;

}

Catch (/ * something * /)

{

delete f;

// whatver else, or resthrow;

}

}

If you build an object in the stack, the life is simple.

// this is a code fragment

{

Foo F;

// All Kinds of Code, Some of Which Might Throw Exceptions}

When F exceeds the range, whether it is an unexpected it will be cleared. This is nature.

When the object is managing it, you can't delete it, it will be cleared by the debris, if you want to clean the object of the source file, you can call the dispose () function, although C # provides a valid structure for it. But it is still not as simple as the stack.

In a new generation of languages ​​(formerly C / CLI), where do you build an object does not rely on the type of object you build, you can manage objects in your stack, it has a determined resolution, but it will be cleared when it is crossed, if you Willing you can build an object in the management stack.

This change brings other results, where you can put different objects in the touchpad or you can see it as other class member variables, you can get a complete C survival time period, not just just It is assigned to the corresponding heap, and then sequentially collected it.

Analysis and finalization

When you write a debris collection object for other languages, do you write a solution for it? When you use C , you can construct the object in the stack, the parsing function will run when it is critical, what happens when other C # or VB programs call this object, run only from simple ways when running, it is with Dispose ) To resolve, any C / CLI object has a resolution function that can be arbitrarily called.

If you have dispose () classes in C # or VB, you may have already written a conclusion, and C / CLI also has a simple syntax as an unique syntax. It is like foo, and foo is the conclusion! Foo (~ is Bit's NOT,! Is logical NOT, they are all reminded that the constructor is opposite),

Finalizer will run when it is created in the management stack, confident that the non-management source files it contains, even if other call functions have forgot to parse.

Pointer and handle

In C extension management, the main limitations of C have no changes, the same symbols and symbols are used for complete things, and the meaning of * depends on the information in your code, you can try the following code:

Foo * pf = new foo ();

Where will the foo object be established? Is that memory cleared? Can I do algorithms on the pointer as below?

PF ;

The answer depends on whether foo uses the __gc keyword declaration, if it is a fragment collection object, it can only be established in the management stack, on the other hand, if there is no __gc declaration, will be in this pile Save it to it, you have to remember to clear it.

If the compiler is free to change the language, it is like what happened on the C / CLI, what type of class that can be ignored, can use a different syntax to indicate where it survives:

Foo ^ hf = gcnew foo ();

This is called a handle, and many C team seem to be indicated by ^ symbols. You can use * and -> to demonstrate the handle, you can get a life period from the declaration of the instance instead of going back statement of. E.g:

Ref class r

{

Private:

INT M_A;

PUBLIC:

R (INT A): m_a (a) {}

}

You may think that REF is a new keyword of C / CLI, but it is not, "Ref class" is a keyword, you can have variables to be ref without causing confusion, other keywords have "Value Class", " Interface Class "," Enum class ", almost all C programs in the past, there will be Value variable, I am very happy that Value does not turn into keywords. Ref class is a management class, a class that is designed into a survival management team and fragment collection management, like you can declare an instance in the front of the stack, the compiler will help you find it, plus invisible Intelligent pointer.

characteristic

There are many C characteristics of characteristics, because I started with C clumsy characteristics, so I now close the relatively similar C

Ref class r

{

Private:

INT m_size;

PUBLIC:

Property Int Size

{

INT get () {return m_size;}

Void Set (int val) {m_size = val;}

}

}

R r;

R.Size = 42;

Is PROPERTY a keyword? A bit icon, it is a location keyword, so you can have the Property variable and function, and it will not cause confusion, it will only have a special meaning in the class definition, and now there is a separate unit in C / CLI. Definition, I like this very much, I believe you too.

转载请注明原文地址:https://www.9cbs.com/read-24215.html

New Post(0)