Home » C Programming » C/C++ programming details (two)

C/C++ programming details (two)

1, static members and class

A, a static member belongs to a class, not to a particular object, which is shared by all objects of that class, so

cannot be initialized in the class's construction method

B, static members belong to that class, all objects are public, and

can be called by class objects

C, private

limit for static members

D, a static member that belongs to the class and all objects of that class. You can call

directly with the class name

2, citing


reference adds another name to the object, which is an existing object, the reference must be initialized, and the type

3, virtual function table, virtual table pointer,

a class with virtual functions. The first four bytes are virtual table pointers and point to virtual table.

class Test{
Int a;
Int b;
Virtual void (fun) {}
Test (int, temp1 = 0, int, temp2 = 0)
Int, getA ()
Return a;
Int, getB ()
Return b;
Int, main ()
Test obj (5, 10);
A and B / / Changing
Int* pInt = (int*) &obj;
* (pInt+0) = 100;
* (pInt+1) = 200;
Cout "a =", "obj.getA ()", "endl";
Cout "B =", "obj.getB ()", "endl";
Return 0;

code output: 200, 10

3, const


c++, declare const int i, at which stage is I readable only.


answer: compile phase.

4, array pointers, and two-dimensional arrays

(1) int a[3][4]. Which one of the following cannot represent a[1][1]?

A, * (&a[0][0]+5)

B, * (* (a+1) +1)

C, * (&a[1]+1)

D, * (a[1]+1)

Answer: C


in a two-dimensional array, a[1] represents the address of the a[1][0], and the array is stored continuously in memory, so the a[1]+1 represents the address of the a[1][1], so D can get the right value;

Pointer operations * (a+1) are equivalent to a[1], so B can also get the right value;

The two-dimensional array is stored in line memory priority, so the a[0][0] address plus 5 in the A can get the correct value;

The C option is wrong. It should be changed to * (&a[1][0]+1), or else to a[2][0].

(two) to make the pointer variable p point to first elements of the 2 dimension group A, the correct assignment expression is ()

p=A or p=A[0]

p=A[0], or p=A[0][0]

p=A[0] or p=&A[0][0]

p=A or p=&A[0][0]

answer: third options,

analysis: A, P = A: in a multidimensional array, the array name is pointed, but not the first element;

B, P = A[0]: in a two-dimensional array, the first element of the two-dimensional array is pointed to;

C, P = &A[0][0]:A[0][0] represents the first element of a two-dimensional array, so it can be p = &A[0][0].

5, inline function

because inline function is relatively simple code embedded into the class, to avoid calling the function result in extra time overhead, generally used to accelerate the speed of the program. Because the code is embedded into the class, it may lead to the executable file of the larger or smaller.

6, new, malloc

a, perform dynamic memory operations on a heap (heap).

b uses the malloc function to specify the number of bytes allocated for memory and cannot initialize the object, and new automatically calls the constructor.

of the object

c, delete calls the object's destructor, and free does not call the object's destructor.

1) malloc function: void *malloc (unsigned, int, size)

In the dynamic allocation area of memory, a contiguous space size of length.

is assigned

if the assignment is successful, the first address of the allocated memory space is returned, otherwise NULL is returned, and the requested memory is not initialized with.

2) calloc function: void *calloc (unsigned, int, num, unsigned, int, size)

According to the number of data given and the number of bytes in the data type, a num * size continuous space.
is assigned
After the calloc has applied the memory space, it automatically initializes the memory space to 0, but malloc does not initialize, and the memory space stores some random data.

3) realloc function: void, *realloc (void, *ptr, unsigned, int, size)

Dynamic allocation of a length of size memory space, and the memory space of the first address assigned to PTR, PTR memory space is adjusted to size.

The memory space for the application will not initialize.

4) new is the operator with dynamic memory allocation, automatic calculation requires the allocation of space in the distribution of types of memory, while calling the class constructor to initialize the memory space, namely, completion of the work class. The dynamic distribution of built-in type is automatically initialized depends on variables defined in the function position, in vitro the definition of the variables are initialized to 0, the variables of built-in type defined inside the body of a function are not initialized.

copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted