Explanation of C ++ programming errors

Source: Internet
Author: User

C ++ programming errors are divided into: focusing on Implicit data type conversion, the structure and object are different, and virtual methods must be explicitly covered, the following describes the errors encountered in C ++ programming. I hope you can get what you want here.

Error 1: Implicit data type conversion

Boxing and unboxing are two processes used to make the value data type be used as the index data type. A value variable can be encapsulated into an object and then unwrapped back to the value variable. Data Types in C #, including built-in data types, can be implicitly converted into an object. Wrap a value-type variable to generate an object instance, and then copy the variable to the instance.

  • Several important elements of C ++ programming tools
  • In-depth analysis of various problems with C ++ development tools
  • On the programming skills and skills of C ++ Programming
  • Analysis of C ++ programming learning and Experimental System
  • Common methods and skills for C ++ Programming

Boxing is implicit. If a variable of the value data type is used where the index data type is required, the value variable is implicitly converted to the variable of the index data type. Boxing will affect the performance of code execution, so we should avoid it as much as possible, especially when the data volume is large.

If you want to convert a packaged object back to the original value type variable, you must unpackage it explicitly. There are two steps to unpackage: First, check the object instances to make sure they are packaged by value variables. Second, copy the values in the instances to the value variables. To ensure successful unpacking, the unwrapped object must be the index of the object generated by packing the value of a value-type variable.

 
 
  1. Using System;
  2. Public class UnboxingTest
  3. {
  4. Public static void Main ()
  5. {
  6. IntI=123; // Package
  7. ObjectO=I; // The Unpacking must be explicit)
  8. IntJ= (Int) o;
  9. Console. WriteLine ("j: {0}", j );}
  10. }

Error 2: The structure and object are different.

The structure and class in C ++ are similar. The only difference is that, by default, the structure access permission is public, and its continuing permission is public. some C ++ programmers use structures as data objects, but this is only a convention, not a necessity. In C #, the structure is only a user-defined data type and cannot replace classes. Although the structure also supports attributes, methods, fields, and operators, it does not support continuation or destructor.

More importantly, the class is an index-type data type, and the structure is a value-type data type. Therefore, the structure is more useful in expressing objects without indexing operations. The structure is more efficient in Array Operations, but less efficient in the collection operations. The set needs to be indexed, and the structure must be packaged for use in the set operation. Classes are more efficient in large-scale set operations.

Error 3: The virtual method must be explicitly overwritten.

In C #, programmers must explicitly use the override parameter when overwriting a virtual method. Assume that A Window class is compiled by Company A. The ListBox and RadioButton classes are written by Company B and programmers Based on the Window class prepared by Company, company B's programmers have little idea about future changes in the Window class. Assume that a programmer of Company B needs to add an Sort method to ListBox:

 
 
  1. public class ListBox : Window  
  2. { public virtual void Sort() {"}  
  3. }   

This is not A problem before Company A releases the new Window class. If the programmer of Company A also adds an Sort method to the Window class.

 
 
  1. public class Window  
  2. { // " public virtual void Sort() {"}  

In C ++, the Sort method in the Windows class will become the basic method of the Sort method in The ListBox class. When you expect to call the Sort method in the Windows class, the Sort method in The ListBox class is called. In C #, virtual functions are always considered as the root of virtual scheduling. That is to say, once C # discovers a virtual method, it will no longer find other virtual methods in the virtual chain. If the ListBox is compiled again, the compiler generates a warning message:

 
 
  1. "class1.cs(54,24): warning CS0114: 'ListBox.Sort()' hides  
  2. inherited member 'Window.Sort()'. 

To overwrite the current member to the original method, you need to add the override or the new one. To eliminate the warning information, the programmer must understand what he wants. You can add new before the Sort method in The ListBox class to indicate that it should not overwrite the virtual method in the Window:

 
 
  1. public class ListBox : Window {  
  2. public new virtual void Sort() {"}   

In this way, the warning information can be cleared. If C ++ programmers really want to overwrite the methods in the Window, they must use the override to explicitly express their intent.

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.