C + + Basics

Source: Internet
Author: User

Preprocessing command: Exists in the xxx.h file

#ifndef// Prevent duplicate inclusions

#define// definition

#endif// End

Precompiled directives are not added to the header file, resulting in a duplicate definition of a compile-time error.

During the actual operation, variables are not typically defined in the header file.

Some functions are not intended to be provided to the user, but are used only for some other function, where the declaration of these functions should not be placed in the header file, but rather defined directly in the source file corresponding to the header file. reflected in the class definition of C + + , these functions should be set to private.

Type conversions:

If there is a double type in the expression , the result is also double(float), if all are of type int , The number of decimal parts is lost when doing division .

Coercion type conversion C : ()a C + +: static_cast< type > ( variable );

Pointer:

The pointer to the first 1 Meanings is the base address of the memory, and the second meaning is the type of itself

int c = 5;

int *a = &c;

A is a pointer to the int type, anda contains the address of C ,& is the address character

declaring and assigning values

int c = 5; Declaration Initialization

int C;

c = 5;// assignment

Variable declaration, function declaration;

  1. the correct switching function
  2. int swap (int *a,int *b)//*a and *b are declared, the following is a,b refers to a two int type pointer;
  3. {
  4. int temp=*a; * is the value operation
  5. *a=*b; Exchange the contents of a and b variables
  6. *b=temp;
  7. }
    In C + + , initialization and assignment are two different concepts. For example:
  8. Initializes an object of the string class with an initial value of HelloWorld
  9. String str1 ("HelloWorld");
  10. error, initializing an empty object that cannot be assigned the same value as the initialization
  11. String str2;
  12. STR2 ("HelloWorld");
  13. correctly, an empty object initialized with the "=" after the symbolic overload is used to assign the value
  14. String Str3;
  15. STR3 = "HelloWorld";
    • In many operations, it is done just by copying a copy, just like the value passed in a function pass. Like what:
  1. from left to right, first connect the str1 to str , and thenconnect thestr2 to str the tail. But the values in str1 and str2 are not changed
  2. String str, str1, str2;
  3. str = str1 + str2;
  4. This is just a copy of STR to the vec .
  5. Vector<string> VEC;
  6. String str (HelloWorld);
  7. Vec.push_back (str);

incorrect application of a one-dimensional dynamic array function

  1. #include <stdio.h>
  2. void Test (char *s)
  3. {
  4. S=malloc (1000);
  5. The initial value of the parameter s is NULL,andmalloc is the first address of an area on the heap, at which point the value of the pointer in the argument does not change. So after the function ends, the argument pointer fails , the essence of the function pass is the value pass, and we want to change the content of the argument, not the content of the argument.
  6. }
  7. two correct notation void Test (char **s) // argument is a pointer, parameter is the address of a pointer
  8. {
  9. *s=malloc (1000); Change the contents of an argument
  10. }
  11. Char *test (char *s)
  12. {
  13. S=malloc (1000);
  14. return s;
  15. }
  16. int main (int argc,char *argv[])
  17. {
  18. Char *s=null;
  19. the incoming function is a copy of s , A temporary variable, a local variable, a copy of the value of a pointer variable, which is the same as the value of two pointer variables. you have to change a variable in the function to pass in the address of the variable here. *xx is just a semantic notation, a statement .
  20. Test (s);
  21. return 0;
  22. }

Dynamically allocating memory

int *p = (int*) malloc (sizeof (int) *10));

sizeof (int) is a cross-platform approach

The real void* returned by malloc need to force type reload

New three ways to apply

1 int *p = new int;

2 int *p = new int (10);

3 int *p = new INT[10];

The difference between malloc and new:

New is an operator, malloc is a function

New executes some type of constructor, and malloc only requests memory.

So for string types, you can only use new, and you must never use malloc!!

Common exceptions are:

The most common problems of exception

Out_of_range Cross-border error

Invalid_argument Illegal parameters

Never return a reference or pointer to a local Object!!!

For example:

Const std::string &MANIP (const std::string &s)

{

std::string ret = s;

return ret;

}

The RET in the example above is a local object that is destroyed when it leaves the function, and the function returns a reference to it, in effect referencing an illegal area of memory.

Dynamically opening up a two-dimensional array 3*4 thinking : first using a double pointer, the double pointer opens up a space of three pointers . each one of the heavy hands opened again 4 a space

    1. int **arr= (int * *) malloc (3*sizeof (int*));
    2. for (int i=0;i!=3;i++)
    3. {
    4. arr[i]= (int *) malloc (4*sizeof (int));
    5. }

The process of freeing space is just the opposite of allocating dynamic space:

    1. for (int i=0;i!=3;i++)
    2. {
    3. Free[arr[i]);
    4. }
    5. Free (arr);

This dynamically allocated array can be passed directly with the void print (int **array) parameter.

function pointer void (* function pointer name) (int, int)

Memory leak memory allocation is used and is not recycled, resulting in the loss of the allocated memory control mode

Value passing, address passing, referencing
    • Value passing means copying a copy to a function's formal parameter during a function pass, with the same value for the argument and the parameter, but the problem is that once the parameter changes, it does not work on the argument. The workaround is to pass or reference the address, or pass the modified value through the return value.
    • Address passing means that the address of a variable is passed in the process of a function pass, which is essentially still the value passed, which is the value of the pointer, the address of the variable.
    • A reference refers to a variable that has an alias. In C + + , for a pass-through operation, a reference is generally used instead of a pointer. Also, be aware that you want to modify the actual parameter value to determine whether to use const to decorate it.

When declaring constants, it is best to initialize the variables first.

Input / output streams istream and ostream are not const when they have buffers as parameters

The difference between a macro function and an inline function:

The macro function is replaced during precompilation.

Inline functions are code extensions during compilation.

An inline function can be considered an advanced macro function, which is checked for syntax.

As long as the macro function does not make a call, it does not produce a syntax check, and the inline function must first undergo a rigorous syntax check for code expansion.

/*

* This generates an unnamed int value when returning int

* This is a temporary value

*/

int Get_val () {

int a = 2;

return A;

}

int main (int argc, const char *argv[])

{

cout << get_val () << Endl;

The value of a gives the intermediate value returned and assigns the value to res

int res = Get_val ();

return 0;

}

Static data member
  • The static data member does not have the This pointer, which is directly subordinate to this class, that is, all objects of this class share this data member. Non- static data members are subordinate to each object of the class, which means that each object owns the data member on its own.
  • It's not really a global variable, it's scoped to just this class.
  • Static data members must be defined outside the class and not initialized by constructors, as normal data members do.
  • The static member function does not have This pointer for the same reason.
  • The static member function cannot be declared as constbecause the static member function is directly subordinate to the class, not the object, and the member function is declared as const is to promise not to modify the object that the function belongs to, which is contradictory.
  • You can call static member functions directly from a class by scope , or, as with ordinary member functions, indirectly through objects, references to objects, and pointers to objects of that class type.
  • The static member function can only operate on static data members, because static member functions are directly subordinate to this class, not static A data member is subordinate to an object, and if it operates on some non- static data members, it affects the security of the object's non- static data members.
Static member function

Non- static member functions can manipulate static data members and non- static data members

C + + Fundamentals

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.