<<c++ Design and Evolution >>
1.c++ 's protection model comes from the concept of access permission and transfer;
The distinction between initialization and assignment comes from thinking of transfer ability;
The const concept of C + + is evolved from the read-write protection mechanism.
2.BCPL notes:
CPL (Combined programming language, Combinatorial programming language): Cpl is a language that is closer to hardware on a ALGOL60 basis. Cpl is large in size and difficult to achieve.
BCPL (Basic Combined programming language, fundamental combinatorial programming language): BCPL is a simplified language for Cpl.
3. Generally in the world, things are not always in good shape, to improve them, there are many things that can be done.
C + + is to solve a problem, not to prove a point of view, and its growth can serve its users.
4. A class is a type.
5. The allocation of objects can be in three different ways:
On the stack (as an automatic object), at a fixed address (static object), or in a free storage area (in the heap, or dynamic storage).
Unlike the C language, the allocation and deallocation of C free storage with classes provides specific operator new and delete.
6.C is intended to be equivalent by name price rather than by structure.
struct A {int x; int y;};
struct B {int x; int y;};
struct A * PA;
struct b* PB;
PA = PB; /* error:a* expected */
PA = (struct A *) PB; /* OK:EXPLICIT Conversion */
So, by name equivalence is the cornerstone of the C + + type system, and the layout compatibility rules ensure that display transformations can be used to provide low-level conversion services.
This exports the "unique definition rule": in the C + + language, each function, variable, type, constant, etc. should have exactly one definition.
6. Trying to ensure that warnings that ignore C + + will be seen as a foolish act.
7. Language design is not the first design, but an art that requires experience, experimentation and an effective engineering compromise.
8.c++ the solution for C Multi-namespace is:
A name can be referred to as a class, but it can also be called a function, or a variable. If a name really refers to two different things at the same time, then the name itself refers to the non-class thing, unless the keyword struct, union, or class is clearly added to the front.
9. Virtual functions are as effective as regular functions in both time and space, and even if there is no virtual function concept, derived classes in C with classes are useful, and can be used to construct new data structures from the old to correlate operations with the results.
10. Object Layout Model
Defines a set of virtual functions defined in a class as an array of pointers to functions, so that a virtual function is simply an indirect call through that array. There is one such array for each class that has a virtual function, which is generally called a virtual function table or VTBL, and each object of this class contains an implicit pointer, commonly called VPRT, that points to the virtual function table of the class of the object.
Class A {
Public
virtual void f ();
virtual void g (int);
virtual void H (double);
Private
int A;
};
Class B:public a{
Public
int b;
void g (int);
virtual void M (b*);
};
Class C:public b{
Public
int C;
void h (double);
virtual void n (c*);
}
An object of Class C looks like this:
+--------------------------+
| A | +----------------------+
| vptr------>| -| &a::f |
| B | | &b::g |
| C | | &c::h |
+--------------------------+ | &b::m |
| &c::n |
+----------------------+
Calls to virtual functions are translated into a reference by the compiled system.
void f (c* p)
{
P->g (2);
}
Become:
(* (P->vptr[1])) (P, 2);
11. Suppressing type checking with ellipses is one of the most intense and least recommended things.
12. Scope of variables:
for (int i = 0; i <; ++i)
{
}
i = 0;
In the C + + language specification, I should not be scoped beyond the for {}, but in vs2003.
The IF, while is also the same.
13. Object-oriented programming is to use the inheritance mechanism of programming, data abstraction is the use of user-defined types of programming, object-oriented programming will be able and should support data abstraction.
14. Type Redefine rule:
typedef char* T;
Class Y {
T F () {t a = 0; return A;}
typedef int T;
};
C + + language rules: If a type has already been used in a class, it cannot be redefined there.
< above T, should be a redefinition, but in the VS2003 error: Can not be converted from y::t to T, because T F () returns the top T, and the body of the function is defined in the class T, so, here is the difference between the error, whether it can not return the different type or the back of the T is a redefinition?>
< This should be a C + + standard and cannot be redefined by type >
15. Rewrite rules: for analysis of member functions that are defined online, as if they were defined after the class declaration.
Such as:
Class TT
{
A f ();
void G () {a A;}
typedef int A;
};
A f (); Is wrong, because A has not yet been declared, and void G () {a A;} is correct because it conforms to the rules written above.
16. Compiler-based error:
Class t{}; If you use T as the class name, an internal compiler error message appears in the vs2003.
17.c++ Definition Rule (1993):
(1). In a class, the scope of a declared name includes not only the body after the name declaration, but also all function bodies, default parameters, and constructor initialization in this class (including nested classes and these parts), but not the declaration of the name itself.
(2). A name used in the class S, when it is re-evaluated in its context or in the full scope of s, must be the same declaration, and the full scope of S is composed of the class s itself, the base class of S, and all classes containing S. This is commonly referred to as "reconsider the rule."
(3). If a member's declaration is rearranged in one class, and another is a legitimate program that conforms to 1 and 2, then the program is undefined, which is often referred to as a "reordering rule."
18. Temporary variables
void f (String s1, string s2)
{
printf ("%s", (s1+ S2). C_STR ());
Const char* p = (S1 + s2). C_str ();
printf ("%s", p);
}
Const char* p = (S1 + s2). C_str (); The specific operation of this sentence:
(1). Generate a String object
(2). Call Copy Construction
(3). Call Overload + =
(4). destructor
So after the P pointer ends, the address of the temporary string object cannot be obtained at all < the lifetime of the temporary object does not go to the end of the function body, because the lifetime of the temporary object ends.
In addition, the + operation in the string library is to return the value of an object, not a reference.
19. Place:
Prerequisites: (1). A mechanism is required to place objects at a specific address (placing an object representing a process on a specific hardware address);
(2). A mechanism is required to allocate objects in a particular area (in shared storage on a multiprocessor system, or in an area controlled by a particular manager).
Solution:
The new operator is overloaded, and the delete cannot be overloaded in C + + rules.
The overloads of new are as follows:
Class X
{
Public
void* operator new (size, void* p) {return p;}
};
void* buf = (void*) 0xf00f; Take a specific address
x* p1 = new (BUF) X; This is where you assign objects to specific addresses.
x* P2 = new (BUF) X; At this time the address of P1 and P2 object is 0xf00f;
The first argument to new is the length of the object, which is passed by the compiler to NEW,BUF, which is the address where the object is placed, and P's reference to the memory allocation function or object.
The above format is the placement syntax format
The placement mechanism makes new a simple storage allocation mechanism because it can be associated with arbitrary logical properties for a particular storage location, making new a common resource management role.
An important application of the allocation area is to provide specific storage management definitions.
Release issue:
It is not possible to expect the user to dispose of the object, and the user will not be able to release the object at all, which is a purpose of the special allocation site.
But you can use P1->x::~x (); Statement release < This is a special invocation format >
So the above class X does not overload the new operator (that's just how the object allocates the memory area).
20.c++ distinguishes 5 "matches":
(1). Do not convert in matches or use only unavoidable conversions (for example, from arrays to pointers, function names to function pointers, and T to const T);
(2). An integer-promoted match is used (as defined by the ANSI standard. That is, from Char to int,short to int and corresponding unsiged type) and float to double;
(3). Matches with standard conversions (e.g. from int to double,derived* to base*,unsigned int to int);
(4). The matching of user-defined conversions is used (including through building constructors and conversion operations);
(5). Use the ellipsis in the function declaration ... The match.
The above can have better matching rules in arm
21.ARM to Pointer Description:
A null pointer is not necessarily represented by the same binary format as the integer 0.
C + + has a strong enough type language, and a concept like a null pointer can be represented in any way that the creator chooses.
So p = 0, the pointer p is assigned a null pointer value, but does not mean that it must be exactly the same as the integer 0.
22. How to implement only one object can be allocated on the heap or how to allocate only one object on a global or stack
(1). Can only be allocated on the heap
Class X {
Public
X () {}
static void Free (x* p) {delete p;}
Private
~x () {}
};
x x; Error
x* p = new X (); Ok
X::free (P); Destruction
It can also prevent the derivation of a new class;
(2). Only one object can be assigned on the global or stack
Class y{
Public
Y () {}
Private
Class D {};
void* operator New (size_t, D) {}
};
Or
Class Y
{
Public
Y () {}
Private
void* operator New (size_t) {}
};
x x; Ok
x* p = new X (); Error
23. How to prevent a class from deriving a subclass
class usable;
Class unable_lock{
Friend usable; Become a friend you can access the private constructor
Private
Unable_lock () {}
};
Class Usable:public Virtual {/* Here must be the virtual keyword */unable_lock
Public
Usable () {}
};
Class Dd:public usable{};
Usable UA; Ok
DD DD; Error, cannot instantiate
Virtual inheritance changes the order of construction: Unable_lock, usable->dd;
Takes the non-virtual inheritance time order: usable, called usable's parent class Unable_lock constructor;
Therefore, when virtual inheritance, the DD class cannot call the Unable_lock constructor, the constructor is private.
If:
Class x1{
Public
X1 () {}
};
Class X2_1:public Virtual x1{
Public
X2_1 () {}
};
Class X2_2:public Virtual x1{
Public
X2_2 () {}
};
Class X3:public X2_1, public x2_2{
Public
X3 () {}
};
X3 xs;
Virtual Inheritance order: X1, X2_1, X2_2->x3;
Sequence of non-virtual inheritance: X2_1->x1, X2_2->x1, X;
There are two roles in virtual inheritance: (1). Change Order, (2). Only one copy of X1, or just one structure at a time.
24. Multiple Inheritance:
Class Aw:public w{};
Class Bw:public w{};
Class Cw:public AW, public bw{};
This is called the multiple inheritance of independence,
Structure:
/----------AW---------W
CW
\----------BW----------W
Virtual Inheritance:
Class Aw:public virtual w{};
Class Bw:public virtual w{};
Class Cw:public AW, public bw{};
Structure:
/-----------AW----------\
CW W
\-----------BW----------/
The "virtual" of class W is a property of the derivation described by AW and BW, not a property of W itself, and each virtual base class always represents an object.
25. The position of a normal base class for each object in a given derived class is fixed.
An object of a virtual base class is not located in a fixed location and must be accessed indirectly. The base class is defined by an unnamed member. If you allow virtual data members, then the virtual base class is an example of a virtual data member.
26. The functions of a derived class are often based on the version of the same function in the base class, which is generally called the method combination.
27. The importance of abstract class concepts is that it enables a clearer division of the user and the implementation, reducing the degree of coupling between the user and the implementing person.
28.const member functions:
The const member function may be used for const or non-const objects, and non-const member functions can only be used with non-const objects.
If there is an object x, this is where the this pointer in the non-const member function of x points to X, and the this pointer in its const member function points to the Const X.
29. Since the static member function is not associated with any particular object, and therefore does not need to be called with the syntax of a particular member function, in some cases the class is simply Lou doing a scope use, putting the name that is not put into the global, which functions as its static member, So that these names do not pollute the global name, which is an origin of the namespace concept.
30. A pointer to a data member is an implementation-independent way of expressing the way that a C + + class is laid out.
A needle pointing to a member is actually more than an offset, which identifies the value of a member in an object.
31.RTTI (run-time type infomation).
32. Runtime type information consists of 3 parts:
(1). An operator, dynamic_cast, gives it a pointer to a base class of an object that can get a pointer to a derived class of that object, and the operator Dynmaic_cast gives the pointer only if it does belong to the specified derived class, otherwise returns 0;
(2). An operator typeid, which identifies the exact type of the object to which a given base class pointer is defined;
(3). A structure type_info, as a hanging point (hook) for dwelling with more runtime types related to the type.
33. Templates:
Template <class t> class vector{
Public
vector (int);
t& operator[] (int);
t& elem (int i) {return v[i];}
};
Explain:
Templates Application template keyword, at the same time for template classes and template functions provide a common syntax form;
<> is to emphasize that template parameters have different properties (will be evaluated at compile time)
Class T is used to indicate the type part of a type parameter
A template is a mechanism that is provided for a build type, is not a type in itself, and does not have a runtime representation, so there is no effect on the layout of the object.
In addition to the type parameters, C + + also allows non-type template parameters, which are basically seen as the information required by the container class to provide size and bounds.
Tempate<class T, int i> class buffer{
Public
Buffer (): SZ (i) {}
Private
T V[i];
int sz;
};
In places where runtime efficiency and compactness are critical, passing size information allows the implementation to not use free space. If you cannot use non-type parameters, the user must encode information about the size into the array type.
Template<class T, Class a> class buffer{
Public
Buffer (): SZ (sizeof (A)/sizeof (T)) {}
Private
A v;
int sz;
};
Buffer<int, int[700]> B;
Namespaces and templates are not allowed as template parameters in the template design.
Type detection is done at the time the template is instantiated.
A template implementation is a mechanism that is based on the user's description of the type that needs to be generated.
34. Class templates can have default parameters, and template functions can not
Template<class T = int> class xyz{}; Ok
Template<class t = int> t Add (t& t) {return ++t;}//error
Because the template parameter of a function template is determined by the type of the argument that the compiler is calling to.
35. In terms of C + + language rules, there is no relationship between the two classes generated by the same class template.
Class x{};
Class Xx:public x{};
Template<class t> class a{};
A<x> X1;
a<xx>x2;
x1 = x2; Error
x2 = x1; Error
36. Member template functions cannot be virtual
Class shape
{
Public
Template<class t> virtual BOOL Get () const {return true;}//error be can ' T virtual
};
Because the virtual function table of a class will add a new item each time it is passed by the consumer, if it can be legalized with virtual. This means that the connection level can be used to construct the virtual function table and to set the relevant functions in the table.
c++--Design and evolution--reading notes