C ++ vs C # (12): function overloading, static modification of class members, and attributes

Source: Internet
Author: User
Tags visual studio 2010

// ================================================ ====================================
// Title:
// C ++ vs C # (12): function overloading, static modification of class members, and attributes
// Author:
// Norains
// Date:
// Monday 04-1_l-2011
// Environment:
// Visual Studio 2010
// Visual Studio 2005
// ================================================ ====================================

 

1. Function Overloading 

In C ++, there is only one principle to overload object functions, that is, the names of derived classes and basic classes must be the same. In addition, there are no more constraints, such:

 

Class cbase <br/>{< br/> Public: <br/> virtual int add () {}; <br/> }; </P> <p> class cderived: <br/> Public cbase <br/>{< br/> Public: <br/> virtual int add (){}; <br/> }; 

The virtual function before the function only tells the compiler that when the base class Pointer Points to the object of the derived class, the Add function of the derived class should be called. If there is no virtual function, in the same case, the call is the add of the base class. However, no matter whether it is virtual or not, the compiler will not give any prompts or warnings.

Compared with C ++, C # is more intelligent in this aspect. If the base class does not use the virtual keyword and the derived class does not use the override, the compiler will have a similar warning:
Warning cs0114: 'function. cderived. add () 'hides inherited member' function. cbase. add ()'. to make the current member override that implementation, add the override keyword. otherwise Add the new keyword.

Don't underestimate this inconspicuous warning. It may be helpful in many occasions. For example, a base class is written by a colleague, and its code needs to call an overload function of the derived class, And you indeed reload it. But in subsequent work, my colleagues think that this function name is not good-looking or has many reasons. If the function name is changed, a disaster will occur and your overloaded function will never be called! With such a warning, it may be possible to avoid similar situations.

C # There are three more modifiers for functions than C ++. The function list is as follows:

Modifier

Description

Virtual

Method can be rewritten

Abstract

The method must be rewritten in a non-abstract class.

Override

Override a method of the base class

Extern

Method definition elsewhere

 

 

 

 

 

2. Static modification of Class Members

 

If static is used in class, C ++ and C # Have a very obvious difference: in C #, static modified functions or variables, it cannot be accessed through objects, but must be accessed using classes. In C ++, there is no such restriction.

Let's take a look at the C ++ code and pay attention to the comments in the Code:
Class cbase <br/>{< br/> Public static int ival; <br/>}; </P> <p> int cbase: ival = 0; // initialize the static variable </P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> int ival = 0; <br/> ival = cbase. ival; // access through a class </P> <p> cbase myobj; <br/> ival = myobj. ival; // access through objects </P> <p> return 0; <br/>} 

But for C #, the situation is indeed quite different, as shown in the following code:
Class cbase <br/>{< br/> Public static int ival = 0; <br/>}; </P> <p> static void main (string [] ARGs) <br/>{< br/> int ival = 0; </P> <p> cbase myobj = new cbase (); <br/> ival = myobj. ival; // This code has an error because static members cannot access it through objects </P> <p> ival = cbase. ival; // only static members can be accessed through classes <br/>} 

I personally prefer the C ++ method, because the class is an abstract level, and the object is a specific stage. If the class can be accessed, the object should also be accessible, after all, static is shared by all objects. It makes no sense to access static members through objects.

There is another difference about static. Careful friends may find that the static variable declaration in C ++ cannot be directly initialized, but must be in. in CPP; as for C #, there is no header file, so there is no external definition, just assign a value directly. C # is better at this point.




3. Attributes

 

There is no difference in attributes. Of course C ++ and C # are not the same, but C ++ is unique to C. C ++ programmers may not understand this.

The so-called attribute is a code block using get and set. It acts on member variables and only works when the operator is "=. It is estimated that everyone will have a headache. Let's take a look at the actual code, as shown below:
Class cbase <br/>{< br/> private int iinternal = 0; <br/> Public int ival <br/> {<br/> Get <br/> {<br/> // return the iinternal variable value <br/> return iinternal; <br/>}</P> <p> set <br/>{< br/> // set the iinternal value <br/> iinternal = value; <br/>}< br/>}; </P> <p> cbase myobj = new cbase (); <br/> int ival = myobj. ival; // call the get method of the ival variable <br/> myobj. ival = 5; // call the Set Method of the ival variable <br/> 

In an inappropriate metaphor, get and set are like overloading the "=" operator for a variable. The C # Attribute method breaks the creed of C ++: All member variables should be declared as private, and the function method should be used for its storage. Of course, the so-called break of C # is actually a form break. In essence, it also follows this belief: if a member variable is directly accessed through an object, when it is assigned a value or obtained does not meet the requirements, use get and set to change its behavior. For C ++ programmers, only myobj. ival is the only way to get an eye out.

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.