C ++ notes

Source: Internet
Author: User
Tags variable scope

1. while (0) Two usage, one is for jump, one is for macro

2. declare that the external variable in the variable uses extern and that the register variable is register.

3. static global variables and static local variables. The difference is that although they are all stored in the global storage area, the Global static scope is the entire file, and the local static variable scope is the declaration area. When the function or statement block defining it ends, its scope ends. After entering again, the value is restored.
Static functions are also called internal functions. They can only be used by the current file and cannot be called by other files.

4. The difference between private static member data and public static member data is that private can only be assigned by the class member method and cannot be assigned elsewhere, including subclass.

5. definitions are inline functions written directly in the class and external functions written on the outside.

6. Class data member pointers and class method pointers: Define pointers to the public data members and methods of the class. If the class myclass has data members public: int c and the member method int cc (), you can define int myclass: * pc, pc = & myclass: c, my. * pc = 2; int (myclass: * pcc) (), pcc = & myclass: cc;

7. pointer array and array pointer. Int (* a) [4], pointing to the array pointer. Int * p [4]. The array element is an array of pointers.

8. Constant pointers and pointer constants.
Char * const pl = string1;
P1 = string2;
* P1 = "this a string"; // valid because the content pointed to by the pointer p1 can be updated.
Const char * p2 = string1;
P2 = string2; // the pointer can be changed.
* P2 = "this is a string"; // invalid. The string pointed to by p2 cannot be updated. Const is a constant pointer (the content is a constant) before *, and a pointer constant after.

9. define common member functions. Void disp () const {}. If it is not an inline function, you must add const at the end of the external function.
Const int I; for a regular data member, the initialization list of the constructor is used for initialization: Myclass (): x (I );

10. add "L", "_ T", "_ TEXT" before the string, L converts ANSI to unicode, and _ T is set according to the environment (unicode/ansi) to determine the conversion result. _ TEXT is the same as _ T. Only _ TEXT and TEXT are windows macros, and __t, _ T, and _ TEXT are VC macros. unicode and multi-byte are two things. Multi-byte and multi-byte refer to windows macros starting with w.

11. The Inheritance Method of the derived class determines the visibility of the base class members on the derived class and the visibility of the base class members on the object of the derived class. Class a: public B: the object of the derived class can access the public members in the base class. The member functions of the derived class and its subclass can access the public members and protect members of the base class. Class a: private B: The derived class can access the public and protected members of the base class and cannot be accessed by the subclass of the derived class. All the members of the base class are invisible to the derived class objects. Class a: protected B: similar to private, the difference is that the public and protected members of the base class are both protected members of the derived class and cannot be accessed by the subclass of the derived class.

12. function parameters include passing values, passing addresses, and referencing. The address is a (int * B); a (& I). Reference is a (int & B); a (I );

13. the operating system gets the message and passes it into the message queue. in the program, Getmeessage gets the message. DispatchMessage first calls windows and enters the canonicalized state (about range 0 ), then, windows calls window functions. Why is it so troublesome? In this way, windows will know what the program is running, and windows will call your window, so that windows will know that you have processed a message when your window returns, if no new message enters the message queue, windows will no longer allocate time slices to your processes.

14. The created window and window class are passed through the class name. WNDCLASSEX. lpszClassName = "a" and CreateWindow ("").

15. messages can be divided into "queue-oriented" and "non-queue-oriented 」. Messages in the queue are put into the program message queue by Windows. In the message loop of the program, return the message and assign it to the window message processing program. Non-queue messages are directly sent to the window message processing program during Windows call window. That is to say, a message in the queue is "sent" to the message queue, while a message in the queue is "sent" to the window message handler. In any case, the window message processing program will obtain all the messages in the window, including queue-based and non-queue-based messages.

16. Difference Between GetAsyncKeyState and GetKeyState. GetKeyState can only obtain the key state during message processing. GetAsyncKeyState is required to obtain the key State outside of the message processing process.

17. char * p = "world"; // note that p points to the constant string p [0] = 'X'; // an error occurs because the constant value cannot be changed. only arrays instead of pointers can be used here: char p [] = "world ";

18: Evaluate the size of the function character array parameter: void func1 (TCHAR abc []) {TCHAR strNum [3]; wsprintf (strNum, _ T ("% I "), wcslen (abc); MessageBox (NULL, strNum, abc, MB_YESNO );}

19. The default parameter value of the function must be placed in the declaration, while the inline function must be placed in the function definition.

20. initialize the const class members in the initialization list.

21. int GetCount (void) const; // const member function. You cannot modify the Function Definition of the data member. const is only used at the end of the function.

22. All string functions should start with _ t as much as possible. (_ tcslen, etc.), the wide character or ansi character will be automatically selected based on the start of _ unicode. If CCH is included, no error is reported, but the string is truncated, such as StringCchCat, StringCchCopy, StringCchPrintf, StringCchPrintfEx, and so on.

23. The global variable is initialized, while the local variable is not initialized and a random value.

24. pointer reference. int I = 111; int * pi = & I; int * & pti = pi; pti is a reference to the pointer. References must be initialized and cannot be defined repeatedly.

25. to modify a non-const member variable for a const Member, declare the member variable as mutable.

26. int I = 20; this is initialization. int I; I = 20; this is a value assignment.

27. The explicit keyword. In C ++, the constructor of a parameter assumes two roles. 1 is a constructor 2 is a default and implicit type conversion operator. So sometimes, when we write code like AAA = XXX, and exactly the XXX type is the parameter type of the Single-parameter constructor of AAA, the compiler will automatically call this constructor at this time, create an AAA object.

28. Call the constructor of the parent class in the subclass initialization list. For example, you want to record the stock market transaction log.
Class Transaction {public: Transaction (); virsual void logTransaction () const = 0 // make a log based on whether to buy or sell };
Transcation: Transaction () {logTransaction ();}
Class BuyTransacton: public Transcation {virtual void logTransaction () const };
Class SellTransaction: public Transcation {virtual void logTransaction () const };
Now, when I use BuyTransaction bs;, because the parent class constructor is the first to call, the logTransactoin () called in the parent class constructor is not a subclass version, during base class construction, the virtual class will not fall to the derived class. in this case, the solution is not to use virtual, And the constructor of the subclass will pass the parameter to the constructor of the parent class:
Class Transaction {public: explicit Transaction (string & loginfo); void logTransaction (string & loginfo) const ;};
Transaction: Transaction (string & loginfo) {logTransaction (loginfo );}
Class BuyTransaction: public Transaction {public Buytransaction (): Transaction (para )};

29. If there are virtual functions in the class, set the Destructor as a virtual function to ensure that the parent class and the subclass can correctly call the destructor. When the base class is set to a virtual function, its subclass and sub-subclass are all virtual functions.

30. in addition to the built-in types, try to pass parameters by reference instead of values for other types, especially when the subclass is passed to a function whose parent class is a parameter, special information will be removed (as if the polymorphism function is lost ).

31. when a child parent has a function with the same name as the parent class, the parent class function is hidden (even if there are different parameters). If you want to hide it, you need to using the parent class in the Child parent class. Public: using Base: mf1; using Base: mf3;

32. virtual functions should be declared as private or protected. Subclass can inherit private virtual functions. The access permission of a private-inherited virtual function can be specified by a derived class.

33. if (I = 0) is false, if (I = 1) is true

34. uchar * c; c + = 2; the pointer moves two places to the right, c [1]; // The content here is the content in the third position.

-------------------------------------------------------------------------------

Connection: http://www.cnblogs.com/i-jie/archive/2009/05/30/1492343.html

 

 

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.