C ++ vs c # (6): entry function, which is used to change the parameter value.

Source: Internet
Author: User

// ================================================ ====================================
// TITLE:
// C ++ vs c # (6): entry function, which is used to change the parameter value.
// AUTHOR:
// Norains
// DATE:
// Wednesday 16-January-2011
// Environment:
// Visual Studio 2010
// Visual Studio 2005
// ================================================ ====================================


1. Entry Functions

The entry function is the entry point for the execution of an application. When the function is executed, the program ends.

For C #, this is a very simple thing. The entry function is Main, which has four forms, such:
Static void Main ()
Static void Main (string [] args)
Static int Main ()
Static int Main (string [] args)

Why are static modifications in front of them? Because C # is a pure object-oriented language, everything is included in the class. The added static identifier is used to indicate that this is a class function, not a member function.

If it is C ++, the situation is complicated. If it is a common C ++ program or DOS program, the entry function is main. That is to say, the form is as follows:
Int main ()
Int main (int argc, char * argv [])

When I see this, some people familiar with C ++ may find it strange that C ++ does not return void? If we strictly follow the C ++ 98 standard, these two forms are actually written in the standard. Some may have asked another question. Why can I still compile normally when I return void? This is actually related to the compiler. For example, if the returned value is void, it can be compiled with VC6.0, while g ++ 3.2 cannot.

The difference between compilers is not the end point. If you are in a Windows environment, the entry function is not "main", but "WinMain! Is it a bit incredible? It is also a C ++ program. After changing the system, even the entry function has changed. But this is the case.

The parameter of the WinMain function has two more parameters than the main function, and the parameter cannot be blank. The format is as follows:
Int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
Int nCmdShow)

Are you finished? If you think this is over, you are too small to look at C ++ in Windows. As Windows migrates data from 16bit to 32bit, the entry function of C ++ also changes. In VC6.0, you can use WinMain, but if it is VS2005 and the compiling environment is UNICODE, you need to use: wWinMain!

The reason is simple. In the ANSI environment, the entry function is WinMain, but in the UNICODE environment, it becomes wWinMain! Can the two environments be directly compatible in the code? Yes, but you need to change the entry function to _ tWinMain!

_ What is tWinMain, so powerful? In fact, it is just a macro, which is defined in the TCHAR. h file:

// TCHAR. H
# Ifdef _ UNICODE
# Define _ tWinMain wWinMain
# Else
# Define _ tWinMain WinMain
# Endif

When we see this, we can see why we can adapt to different compiling environments?

2. Change the parameter value

In C ++, if you want to input the value of the form parameter, what is the method? There are two methods: pointer and reference.

It is more straightforward to illustrate with examples. We need a function to exchange two values of the input parameter. This is a simple example, isn't it?

Assume that the following two variables are declared in the Code:
Int a = 10;
Int B = 20;

The reference method is as follows:

Void Swap (int & a, int & B)
{
Int tmp =;
A = B;
B = tmp;
}
// Directly input the form parameter.
Swap (a, B );

If it is a pointer, it is also very simple:

Void Swap (int * pA, int * pB)
{
Int tmp = * pA;
* PA = * pB;
* PB = tmp;
}

// Use the & symbol to indicate that this is a pointer during the call.
Swap (& a, & B );
 

What is the difference between the two methods of reference and pointer? If there is no difference from the functional perspective, they can all complete the exchange task. From a philosophical perspective, the pointer method can indicate that the form parameter can be NULL, reference indicates that the parameter cannot be NULL. Is that a bit mysterious? No way, the complexity of C ++ is shown here. C ++ is not so much a language as a belief or philosophy, but may be more appropriate. To put it simply, why do C ++ have these two methods and trace the source code is actually compatible with C. References are new features introduced by C ++, which are not available in C. If C wants to change the value of the form parameter, the pointer can only be used. In order to be compatible with C, there must be a way to change the shape parameter in C ++.

However, if C # is used, the pointer is removed, so it only references this mode. Of course, the reference of C # is different from the writing method of C ++. C ++ is represented by the & symbol, while C # uses the ref keyword. The biggest difference is the function call. C ++ directly enters the variable name, while C # must add the ref keyword. The Code is as follows:

Static void Swap (ref int a, ref int B)
{
Int tmp =;
A = B;
B = tmp;
}

// The call must contain the ref keyword
Swap (ref a, ref B );
 

Initially, it seems that adding the ref keyword to the C # Call reference is a little cumbersome. In fact, in actual use, this is slightly cumbersome but brings a clearer meaning. For example, in C ++, if you only check the function call: Swap (a, B), can you determine whether the two parameters a and B will be changed? No matter whether it is referenced or not, the call method is the same. Therefore, you must return and view the Swap definition before you can determine. This ambiguity does not exist in C #. The value of a and B will never be changed in the Swap (a, B) statement. If there is a possibility of a change, the ref annotation must be used, that is, the call method of Swap (ref a, ref B.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.