C + + Easy error point

Source: Internet
Author: User

    1. Q: Void print (int arr[][], int size); Is this function declaration right or wrong?

      Of course it is wrong, this is not a function of the declaration of the problem, but the problem of the array declaration, int arr[][] This statement is wrong, the length of the latter dimension can not be omitted!

      Q: char* screeninit (int height = +, int width, char background); How about this statement?

      Wrong, if you specify a default value for the parameter height, the subsequent parameters must both specify a default value.

      functions defined inside the class default to the inline class

      How to use C + + pointers

      Why use both the * and & symbols in the following function declarations? And on what occasions to use this declarative approach?
      void Func1 (MYCLASS *&pbuildingelement);

      Such questions are often asked in the forum. This article attempts to explain this problem by using some practical pointers to experience.
      It's a bit confusing to take a closer look at this way of declaring. In a sense, "*" and "&" are two things that are relative to each other, what is the point of putting them together? In order to understand this approach to pointers, let's review the ubiquitous pointer concept in C + + programming. We all know what myclass* means: A pointer to an object that is of type MyClass. Void func1 (Myclass*pmyclass);

      For example: myclass* p = new MYCLASS;
      Func1 (P);
      This method of handling the above code must have been used, create a MyClass object, and then pass it to the FUNC1 function. Now suppose this function wants to modify PMYCLASS:VOIDFUNC1 (Myclass*pmyclass)
      {
      DoSomething (Pmyclass);
      Pmyclass =//pointers to other objects
      }

      The second statement modifies only the value of Pmyclass during the function. The value of the caller's variable p is not modified. If P points to an object at address 0x008a00, it still points to this particular object when Func1 returns. (This is entirely possible unless the func1 bug will mess up the heap.) )

      Now suppose you want to modify the value of P in func1. This is your right. The caller passes in a pointer, and the function assigns a value to the pointer. It used to be a double pointer, a pointer to a pointer, for example, cmyclass**.


      myclass* p = NULL;
      Func1 (&P);

      VOIDFUNC1 (Myclass**pmyclass);
      {
      *pmyclass = new MYCLASS;
      ......
      }


      After calling Func1, p points to the new object. In COM programming, you will encounter this usage everywhere-for example, in the QueryInterface function of the query object interface:


      Interface ISomeInterface {
      HRESULT QueryInterface (Iid&iid, void** ppvobj);
      ......
      };
      Lpsomeinterface P=null;
      Pob->queryinterface (IID_SOMEINTERFACE,&P);

      Here, p is a pointer to the Someinterface type, so &p is a pointer to the pointer, and if the call succeeds when the QueryInterface returns, the variable p contains a pointer to the new interface.

      If you understand pointer pointers, then you definitely understand pointer references, because they are completely the same thing. If you declare the function as follows:


      void Func1 (Myclass*&pmyclass);
      {
      Pmyclass = new MYCLASS;
      ......
      }

      In fact, it is matter with pointers to pointers in the previous example, except that the syntax is different. Pass the time not to preach P address &p, but directly to the P itself:

      myclass* p = NULL;
      Func1 (P);

      After the call, p points to a new object. Generally speaking, the principle of reference is more or less like a pointer, syntactically it is a common variable. So as long as you meet *&, you should think of * *. This means that the function modifies or possibly modifies the caller's pointer, and the caller passes the pointer like a normal variable without using the address operator &.

      As for what occasion to use this method, I would say, very little. MFC uses it in its collection class--for example, CObList, which is a list of cobjects pointers.

      Classcoblist:public Cobject {
      ......

      Gets/modifies the element at the specified position
      cobject*& GetAt (positionposition);
      cobject* GetAt (positionposition) const;
      };


      There are two getat functions, all of which are the elements that get the given position. What's the difference?

      The difference is that one lets you modify the object in the list, and the other does not. So if you write the following: Cobject*pobj = MyList. GetAt (POS);

      POBJ is a pointer to an object in the list, and if you then change the value of POBJ: POBJ = psomeotherobj;

      This does not change the object address at POS, but only changes the variable pobj. But if you write the following: cobject*& rpobj= mylist. GetAt (POS);

      Rpobj is now a pointer to an object in a list, so changing the rpobj will also change the address of the object at POS in the list-in other words, instead of the object. That's why CObList has two getat functions. One can modify the value of the pointer, and the other cannot. Notice what I'm saying here is the pointer, not the object itself. Both functions can modify the object, but only the *& version can replace the object.

      It is important to refer to C/s + + and it is also an efficient means of processing. Therefore, in order to become a C + + master, the concept of reference is not a thorough understanding and skilled application is not possible.

    2. Inline is a recommendation to the compiler that the function is placed in the header file and added at the time of definition, and the method body written directly at the class declaration is equivalent to the addition of inline;
    3. struct, class, enum, union the last side remember to add a semicolon, namespace finally no semicolon;
    4. The default parameter is used when declaring, the default parameter must be behind;
    5. Virtual is declared in the parent class to make sense, and it is only a mistake to declare it in a subclass. Virtual in the subclass is optional, and is generally used to mark it;
    6. extern is used at the time of declaration, which means that the function defined elsewhere is extern, but the variable is added by itself;
    7. C++03 class templates can have default parameters, but the function template cannot have the default template parameters, C++0x provides the function default template parameter support;
    8. Static modifies the variable in the file to indicate that the variable is not visible in other files;
    9. The case of a switch statement can only be a constant number (enumeration) or a character;
    10. Symbolic shift operations and unsigned shift operations are different, and are divided into arithmetic shifts and logical shifts;
    11. Add when macro is defined
      [CPP] view plaincopy
      1. #define MACRO do{//todo}while (0)
      To prevent the creation of errors;
    12. The Const class member functions are initialized in the initialization list, and static member functions need to be initialized outside the class, and const static can be initialized directly in the class or as static when defined outside the class;
    13. Only the return value is different and cannot be overloaded;
    14. The problem of ambiguity needs attention;
    15. Delete NULL is legal and does not produce an error;
    16. The step size for adding and subtracting a pointer depends on the type it points to;
    17. The use of addition to exchange two values is prone to overflow, can be used XOR to do;
    18. The directly assigned char pointer is a constant string and cannot be modified;
    19. The contents of the pointer passed into the function can be modified, the modification to it is wrong, and the pointer must be passed when needed;
    20. Note the extensions provided by the compiler are distinguished;
    21. C + + has a stronger type check than C, some do not need type conversion in C, in C + + need to display the use of type conversion;
    22. The return value of the function and the passing of the parameter will result in a copy;
    23. A reference to a local variable is returned, or the pointer is wrong, and the return value is correct because it returns a copy;
    24. member function pointers are strongly typed, conversions are required to display transformations, and static member functions can be accessed directly with normal function pointers;
    25. Static member functions cannot use virtual, const, and volatile modifiers, and static functions are non-existent virtual function tables for classes so they cannot be virtual (the other two don't know why);
    26. Static member functions can only manipulate class variables without the this pointer;
    27. Dynmaic_cast only supports pointer and reference type conversions, and does run-time type detection, and other conversions do not;
    28. The results of sizeof are related to byte alignment and pack;
    29. sizeof an empty class result is 1;
    30. [CPP] view plaincopy
      1. #ifndef MACRO
      2. #define MACRO
      3. #endif
      Only duplicate inclusions can be resolved and duplicate definitions cannot be resolved;
    31. The template's export keyword is not supported, and the detach definition model is not supported;
    32. Goto is a good thing;
    33. The Boost library works well, but it's easy to use wrong;
    34. You can use parentheses to prioritize operator precedence;
    35. STRING::C_STR () returns the const char*;
    36. Keep the function member variable declaration order and initialization list in the same order;
    37. Only a pointer reference, no pointer to the reference;
    38. Because C + + in the name of the C function library has been modified to do the overloaded features, C + + use of the library is required to add extern ' C ' in front of the function;
    39. The const function can only call a const function and cannot call a non-const function;
    40. -,.,:: Priority is higher than *,&;
    41. Do not omit header files that should be included because of compiler support;
    42. The types defined by the struct, enum, and union in C require these keywords when declaring variables, whereas in C + + they are not required;
    43. You can nest enum in C + + to qualify it, but in c it seems to be invalid;
    44. Do not add the name to the enum when it is used, and the members of the enum are global;
    45. A const member function cannot call a very member function;
    46. Constant member objects can only call constant member functions, except constructs and destructors;
    47. Shift operations are divided into logical shifts (SHL/SHR) and arithmetic shifts (sar/sal), while the unsigned number of shift operations in C is a logical shift, and the signed number is an arithmetic shift;
    48. function overloading, template deduction only for parameters, if the return value is the template needs to display the specified type;
    49. In order to remain portable, the path is used/to be segmented instead of \;
    50. Note the path and the case of the file.
    51. When nesting templates, be careful to split the angle brackets with a space, even if the c++11 has been first interpreted as nested templates;
    52. The wchar_t used by the Windows platform may have compatibility issues on the Linux platform;
    53. Only the argument is a const reference to pass the return value directly to the argument, and you need to declare a variable for him (http://blog.csdn.net/cnsword/article/details/7231205);
    54. When a signed number is converted to a type, it is filled according to the high fill, that is, the highest bit is 1, the high position of the fill will be 1, which will cause the change of the value;
    55. If a subclass has a function with the same name as the base class, it is hidden if not overloaded/overwritten, and if it is to be used, the using Base::fun will be displayed;
    56. A class that has a polymorphic relationship, the base class destructor must display the definition, and it should be virtual.
    57. Structures that contain C + + types (such as std::string) do not allocate memory with malloc;
    58. GCC uses Unicode compilation under Windows to add-d_unicode macro definitions

C + + Easy error

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.