Rookie C ++

xiaoxiao2021-03-06  60

2004/2 --------------------------------------------- -------------------------------------------- How to debug register? Here is a example:

Typedef struct regs_t {dword eax; dword ebx; dword edx; dword esp; dword ebp; dword ESI; DWORD EDI;

#define show_regs (x) regs __regs; / __ASM {mov __regs.eax, eax} / __asm ​​{mov __regs.ebx, ebx} / __ASM {mov __regs.ecx, ECX} / __asm ​​{mov __regs.edX, EDX} / __ASM {mov __regs.esp, esp} / __asm ​​{mov __regs.ebp, ebp} / __ASM {mov __regs.esi, esi} / __asm ​​{mov __regs.edi, edi} / char __regs_text [128]; /

2004/8/20 --------------------------------------------- ------------------------------------------- Int * p, q The first look, it seems that P and Q are int * type, but in fact, only P is a pointer, and Q is a simplest INT type variable. INT (* p) [4] = rollnum; int * q [5];

Here, P is declared as a pointer to a 4 element (INT type) array, and Q is declared as an array containing five elements (INT type pointers).

In addition, we can also mix practical * and &, as follows:

INT ** P1; // p1 is a pointer to a pointer to an int.int * & p2; // p2 is a reference to a pointer to an int.int & * p3; // error: Pointer to a reason is Illegal .int && p4; // error: Reference to a reference is Illegal.

Note: P1 is a pointer for an int type pointer; P2 is a reference to an int type pointer; P3 is a pointer (not legal!); P4 is a reference to an int type reference (not legal!). Const Int * P; int const * q;

Which of them represents a constant type pointer (Const Directly Modified Int), which one represents an int type const pointer (Const Direct Function Pointer)? In fact, P and Q are declared as a constent Int type pointer.

2004/9/1 --------------------------------------------- -------------------------------------------- 1 for_each and expressions Expression Template ?????? 10 DD2 pure virtual private function?

2004/9/2 --------------------------------------------- -------------------------------------------- Microsoft style is IF ( 0 == i) This kind of case can be prevented from writing if IF (i = 0) so if the statement after IF is affirmed 2004/9/2 -------------------------------- -------------------------------------------------- ------------------------ from 9CBS assumption INT A [10] p1 = a; then * p = 1; * P = 2; * P = 3; (* p) = 4; How should I follow the priority? According to the statement (back)> (front)> * Decodes> = equal to * P should be a calculation (P ) in the end assignment? See all the correct algorithms and answered -------------------------------------------------- ------------ (suffix) belongs to "Suffix Operation", which is higher than "prefix operator". * And (prefix) are "prefix operators", the same priority, press the order from left to right. It is higher than the assignment operator. So: * P = 1 corresponds to (* (p )) = 1, that is, is operated on P, the result is the original value, then do * operation, remove the reference, and then assign it to 1. The total role is to assign a value of the P reference to 1, and P plus P plus 1. * P = 2 corresponds to (* ( P)) = 2, that is, is operated on P, the result is the value of P plus 1, then the * operation, remove the reference, and then assign value 1 . The total role is to put P plus 1, and then assign a value of 2. * p = 3 corresponds to ( (* p)) = 3, that is, the first to P-based * operation removes the reference, the result is the object of the P reference, then this object 1, the result is still this object , Assign it to 3. This expression requires the return value of the prefix operator of the object to the left value. (* p) = 4 There is a forced super-level (parentheses), its level, result is ((* p) ) = 4, that is, the first to P-based * operation removes the reference, the result is The object is referenced, then the object is a suffix operator, the result is the value before this object (generally a temporary variable), then assigns it to 4, this expression requires the subject's suffix operator The return value is the left value (the integer type does not meet the requirements, "only the object type defined this operator is likely to meet the requirements).

This problem is difficult to test in C, which can be clearly used in C . The method of operator is overloaded (operator overload does not change priority): #include class test {public: test () {} test (int) {} Test & Operator = (const test &) {std :: cout << "assignment of test << std :: end; return * this;} test & operator () {std :: court <<" prefix Of test "<< std :: endl; return * this;} Test & Operator (int) {std :: cout <<" suffix of test "<< std :: end1; return * this;}} Class testptr {test value; public: testptr & operator = (const test &) {std :: cout << "assignment of testptr" << std :: end1; return * this;} testptr & operator () {std :: cout << "prefix of testptr" << std :: end1; return * this;} TestPtr & Operator (int) {std :: cout << "suffix filter" << std :: endl; return * this;} test & operator * () {std :: cout << "Operator * of testptr" << std :: end1; return value;}}; #define track (x) std :: cout << std :: Endl << "*****" << #x << "*****" << s TD :: end1; x int main () {testptr p; track (* p = 1); track (* p = 2); Track ( * p = 3); track ((* p) = 4); std :: cin.get ();} Output ***** * p = 1 ***** SUFFIX of testptr operator * of testptr assignment of test ***** * P = 2 ***** prefix

Of testptr assignment of test ***** * p = 3 ***** Operator * of test proptr prefix of test assignment of test ******** (* p) = 4 ***** Operator * of test proptr suffix of test assignment of test int P = 1; int A = p ; result A = 1, not because suffix priority is low (I remember that there is a C text material is like this Written, it's really misused), but is determined by the suffix . The standard suffix should be "do 1 operation objects, and return the value before the operation", it is calculated before the assignment, but its return value is not P, but P is doing 1 operation Value. So we can also know that the return value of the P should not be a left value, and P = a is unable to compile. The prefix is not the same, P is the meaning of 1 operation, and return P ", the return value is P itself (reference), is a left value, p = a is compilant (But nothing). If you use the code to describe these two operators, it should be like this: const INT INT :: Operator (int) // suffix {int TEMP = * this; * this = * this 1; Return Temp; } Int & int :: Operator () // Prefix {* this = * this 1; return * this;} Supplement: In C, the above statement meaning is: * p = 1; -> TEMP = P 1; * TEMP = 1; * P = 2; -> p = p 1; * p = 1; * p = 3; -> * p = * p 1; * P = 3; (* p) = 4; // syntax error, unable to write the corresponding statement. Because of the particularity of the suffix / reduction operator, it is difficult to understand the initiator "why the suffix is ​​ priority high, but the variable adds 1?" In fact, in fact, "suffix " is not Plus the variable plus 1, but do it first, but its return value is not this variable, but the value before this variable changes.

If it is difficult to understand, it is recommended not to use these operators, and switch to ordinary plus / subtractive operators: * p = 1; -> * p = 1; p = p 1; * P = 2; -> p = p 1; * p = 2; * p = 3; -> * p = * p 1; * p = 3; (* p) = 4; / / Syntax error, unable to write the corresponding statement. Since these operators in C are no longer unique to integers and pointer types, but they can be defined for classes, and they can be different or no / subtractive operators, such as bidirectional iteration. ), Do not simply replace / subtract. However, C programmers can truly understand the semantics of the increment / reduction operator by watching the operator of the class, if the iterator's code is true, it will not be added to the variable because it "priority is high." The problem is confused. However, only this, I still think that it is best to use an increase / reduction operator to make an expression or there is no increase / reduction operator, or only one increase / reduction operator, such as p; * P = 1; (equivalent to * p = 1) or * p = 1; P; (equivalent to * p = 1), this is not to distinguish between prefixes and suffixes. 2004/9/2 --------------------------------------------- -------------------------------------------- I already contain Objbase Coinitialize (0), an error is as follows: 'CocreateInstanceex': undeclared Identifier ------------------------------- -------------------------------- Do you have defined _win32_dcom or w _ _ 0x400? ---- -------------------------------------------------- --------- stdfax.h plus this #define _win32_winnt 0x0500

AFXDUMP is a global variable for cdumpContext types

2004/20 --------------------------------------------- -------------------------------------------- Structural compilation alignment VC C1 / zp1 (or porject-> settings-> c / c -> code generation-> struct menber alignment) BCB BCC32 -A1

2004/11/21 ------------------------------------------- --------------------------------------------- 1 Try {throw "dsfsdfsdf");} catch (float x) {printf ("Hello World! / N");} catch (char * x) {printf ("Hello World! / N"); // Execute this sentence} 2 TRY {Throw ("DSFSDFSDF");} catch (float x) {printf ("Hello World! / N"); // Execute this sentence} 3 try {throw (1);} catch (float x) {printf (" Hello world! / N ");} catch (int x) {printf (" Hello World! / N "); // Execute this sentence} 4 try {throw (1);} catch (float x) {printf (" Hello World! / N "); // Execute this sentence} It seems that the Catch of Catch in c is not like the objects in the CPP. 5 try {throw (1);} catch (...) {Printf ("Hello World! / N");} catch (int x) {printf ("Hello World! / N");} Compile error! Catch (...) must be in the last 2004/11/23 ------------------------------------- -------------------------------------------------- --- "Lingbo Microchemical" Open Speed ​​Optimization Switch: VC C1 / O2Linux G -O2 Optimized instruction 1 Change the implementation of the FOR loop to Do WHILE Structure 2 More Utilization System Register 3 Optimize Switch I will have to be quick! See P140, system function strlen () implementation (VC98 / CRT / SRC / Intel / Strlen.asm). Each time compare 4 bytes VS each time compare 1 bytes 2006/1/12 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- -------- Virtual inheritance test: Class Point {public: int x;}; class point3d: public point {}; class vertex: virtual public point {}; class vertex3d: public point3d, public vertex {} Class PVERTEX: Public Vertex3d {}; int main () {Vertex3D v; v.point3d :: x = 1; v.Vertex :: x = 2; Printf ("% D% D / N", v.point3d: : x, v.vertex :: x); getChar (); return 0;

Output 1 2 If it is a class point3d: Virtual public point {}; output 2 2

1/18/2006

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

IT's One of the Classic Basic Forms for Malloc Arguments: 1 Object_ptr = Malloc (n * sizeof * Object_ptr); 2 string_ptr = malloc (String_length 1);

3 void_ptr = malloc (sizeof variable);

Notes: 1 The Parenthesis Is Not Necessory for SizeOf; 2 The * Position

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

1/26/2006

foo * and const foo * are different types but are nevertheless similar enough that you can use const_cast to cast between them Where two types are used as template arguments, however, the similarity of the types is irrevant Consider..:

Template Class Test {T T;

}

Template <> Class Test {char Array [1000];

}

Thus test Contains an Array of Chars, WHEREAS TEST Contains a Single Int.

#include using namespace std;

INT main () {test T1; test T2; cout << sizeof (t1) << endl; // gives 4 cout << sizeof (t2) << endl; // gives 1000 returnograph ;

}

In reality vector and vector may hardly differ at all --- in particular, they may have the same size. However, the possibility of explicit template specialization means that they could differ spectacularly, hence the reluctance of the compiler to allow conversion.

-------------------------------------------------- ---------------------------------------- 1/27/2006

You should not pass temporary objects by reference unless the reference is a const. Because by the time the code gets to look at the non const temporary object that was referenced, the temporary object may or may not exists.

2/9/2006 About Stream ------------------------------------------- ---------------------------------------------- There's An idiom (and a supporting member function) for checking the state of a stream, which allows a stream object to be used in a boolean context. But converting it to a boolean value does 'just happen' not. to do so requires the use of A Boolean Logical Operator (A stream's 'value' itself is not boolean). (The Member Function IS 'Operator Void *', You Can Research It if you like).

Try this

IF (! outfile) // stream in is error state else // stream is in 'good' State

If you want 'good' State, You Can Write:

IF (!! outfile)

OR:

IF (Outfile.Good ())

BTW if You Only NEED OUTPUT, You Should Use 'std :: OFSTREAM'. 'Std :: fstream' is intended for streams Which is Both Read from And Written TO.

转载请注明原文地址:https://www.9cbs.com/read-88605.html

New Post(0)