C / C ++ in some small things to note

........

1, int * p = new int (5); an address corresponding to the pointer p is stored inside 5.

      int * p = new int [5]; apply a dynamic array of length 5, inside the uncertainty value.

2, vector is not a class, but a class template.

3, in the derived class to be displayed if the constructor calls the base class is initialized, the presence table with parameters BenQ constructor class, if the base class constructor with no arguments, then no longer display the derived class to call the default call the base class constructor without parameters inherited system data is initialized.

Sequence derived class constructor is performed as follows:

      1) base class constructor execution order in order of the time they inherit.

      2) Next, the new members of the derived class to initialize the object, according to their order of declaration in the class inside the call.

      3) Finally, the implementation of the derived class constructor inside their own stuff.

4, the scope operator to distinguish members of multiple inheritance is repeated when the base class inherited problems.

such as:

      class A 

      {

       public :

            int i;

            void func();

      };

      class B 

       {

        public :

             int i;

             void func();

        };

       class C : public A ,public B

       {};

       C c;

       c.A::i=1; 

       c.A::fun(); 

       Or by such declaration in a derived class:

       class C : public A ,public B

       {

        using A ::i;

        using B::fun();

        }

5, virtual base class is to solve the rhombus pattern inherited data members or member functions when multiple inheritance, this virtual base class can only be stored in a form memory. Character may be used to distinguish them :: scope, as mentioned above 4 as a virtual base class may also be used.

class A 

{

public :

int a;

void fun ();

};

class B : virtual public A 

{};

class C : virtual public A 

{};

class D : public B ,public C 

{};

D d ;

da = 1; A virtual base class data members of the access //

d.fun (); virtual function member A base class // access

6, a base pointer can be converted by converting the display pointer to the derived class, the base class object but can not be directly converted to the class object (or its reference type) constructor parameter unless the derived class to accept the base class.

A a;

B b =static_case<A>(a);    错误。

7, static binding and dynamic binding is early binding and late binding, corresponding to the polymorphic implemented in two ways.

8, the operator overloading front and rear ++ ++ above, the presence or absence of a difference parameter int, int in the no practical effect, but in order to distinguish front and rear ++ ++

eg:

      A operator ++ (int) Rear be overloaded unary operator

      A operator ++ () Pre unary operator overloaded

9, the virtual function that is used in the above polymorphisms, derived classes added to modify some of the behavior of the function of the base class, then put the function in the base class virtual function is modified, and then overridden in a derived class.

     Most should be emphasized that: as long as the base class through a pointer or reference to the virtual function call, only dynamic binding can occur, can not be achieved through the object base class. the reason:

Pointer can point to an object base class to the derived class, the base class can be quoted as an alias derived object, but the object does not represent the object base class derived class.

10, can not be declared virtual constructors, but may declare virtual destructor, to a pointer to the base class in some cases, the space below the derived class derived class open release.

11, as long as the class with a pure virtual function is an abstract class, the derived class if not implement all pure virtual functions is given a base class, the derived class belonging to this time is an abstract class. An abstract class can not be instantiated, the object is not defined, but may define a pointer to the abstract class and references.

12, a deep copy & shallow copy: If there is a data member of a class is a pointer type, the need to relate to deep copy, or else will result in the assignment if the object constructor call, no real assignment, the element simply the first address assignment, and avoid calling the destructor when the object after the assignment, on the same piece of memory twice. Hence the need to manually write a copy constructor (to the need for open space were opened, releasing space for release).

13, if you want to write a class can not be inherited, because a class can not be defined objects, in fact, is to see the constructor, so the definition of class, if the constructor on private data, then the equivalent of the class does not make sense, inherited You can not call the constructor. But the definition of this class is no problem, but no.

14, compile-time polymorphism: syntax check is performed to determine which function is called, overloading is this, then just been compiled, the program code converted into object code, and no operating system to connect, not run.

       Runtime polymorphism: After compilation, a connection with the operating system and the operating system is running, polymorphic carried out in this period is a multi-state operation, the rewrite (overwrite) is one such.

15, polymorphism in the virtual function table:

       Simple Description: Each class has a virtual function table, with the presence of an object class definition will point to a pointer to the virtual function table (i.e., virtual function table pointer), so no matter how the object converter, each time it calls the function They are the same.

16, peekmessage and getmessage difference:

       Will return when the difference between asynchronous and synchronous, peekmessage are taken from the message queue, whether or not the message will be returned directly, but getmessage message queue if there are no messages, it will have to wait, knowing that the message queue has a message .

Published 23 original articles · won praise 4 · Views 9967

Guess you like

Origin blog.csdn.net/hxp1994/article/details/89824938