C ++ vs C # (4): enumeration, struct

Source: Internet
Author: User
Tags visual studio 2010

// ================================================ ====================================
// Title:
// C ++ vs C # (4): enumeration, struct
// Author:
// Norains
// Date:
// Tuesday 7-December-2010
// Environment:
// Visual Studio 2010
// Visual Studio 2005
// ================================================ ====================================

 

1. Enumeration

 

Both C ++ and C # Use Enum to declare an enumeration type, for example:Enum format <br/>{< br/> rgb888, <br/> rgb565, <br/> };

However, they are quite different in terms of use. For C ++, you can directly use the defined type, while C # must add a. identifier, such:// C ++ <br/> format = rgb888; </P> <p> // C # <br/> format = format. rgb888; <br/>

In this case, C # seems to be cumbersome and requires corresponding Type Definitions. However, this brings another benefit: Different Enum types in the same namespace can have the same value, for example:Enum format <br/>{< br/> rgb888, <br/> rgb565, <br/> }; </P> <p> Enum format2 <br/> {<br/> rgb888, <br/> rgb565, <br/>}< br/>

This code can be compiled smoothly in C #. the following error is prompted in C ++: Error c2365: 'rgb888': redefinition; previous definition was 'enumerator };

 

In C ++, Enum is essentially a set of numerical values, and its name is only the identification of numerical values. However, for C #, it is a step forward, enum can also obtain the corresponding name string, such:String strformat = format. rgb888.tostring ();

You can use not only member functions, but also global convert functions, such:String strformat = convert. tostring (format. rgb888 );

More interestingly, in addition to the numeric type, the string type can also be converted to the enum type, for example:Format = (Format) enum. parse (typeof (format), "rgb888 ");

Note that the string here is also case sensitive. If the converted string is not in the enum type name column, an error occurs.

 

2. struct

 

In C ++, the class and struct are consistent. The only difference is that the default access domain of the class is private, while the class is public. However, for C #, the default access domain of struct members is private. To specify public, each member variable must be specified, for example:Struct size <br/>{< br/> Public int X; <br/> Public int y; <br/> };

If you are familiar with C ++, you may think it is a little troublesome to write this code. Can C # Be like C ++:Struct size <br/>{< br/> Public: <br/> int X; <br/> int y; <br/> };

It is a pity that this writing method is not true in C. Simply remember, C #'s structure syntax can be fully transplanted to C ++, and vice versa.

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.