C ++ object model-differences brought about by keywords (Chapter 1), chapter 1 of Object Model
1.2 If A Keyword Distinction is not used to maintain compatibility with C, C ++ can be simpler than it is. For example, if there are no eight Integers to support, the overloaded function solution will be much simpler. Similarly, if C ++ loses the C Declaration syntax, it does not need to judge that the following row is actually a function call operation (invocation) of pf rather than declaration:
// Do not know whether the following is a declaration or invocation // the int (* pf) (1024) can be determined until the integer constant 1024 is seen );
In the following statement, "lookahead" as above does not even play a role:
// Meta-language rule: // declaration of pq, instead of invocationint (* pq )();
When the language cannot distinguish whether it is a declaration or an expression, a rule beyond the language range is required, and the Rule judges the above expression as a "Declaration ".
Similarly, if C ++ does not need to support the original struct of C, the concept of class can be supported by the keyword class. But it is surprising that, apart from efficiency, the other most frequently asked by programmers is: When should I replace the class with struct in the C ++ program?
The keyword struct does not have to represent anything that is subsequently declared. You can use struct to replace the class, but still declare access segments such as public, protected, and private, and a completely public interface, as well as virtual functions, as well as single inheritance, multi-inheritance, virtual inheritance, and so on.
In C ++, select struct or class as the keyword to import the ADT on the grounds that the code is sound.
There is an important conceptual difference between the struct supported by C and the class supported by C ++. Focus:
The keyword itself does not provide such a differenceIf the following definition type is used, it can be said that this is a class.
// The struct name (or class name) is temporarily omitted {public: virtual void foo (); protected: static int object_count ;};
In fact, it can be a struct, or a class. The conceptual meaning of these two statements depends on the test of the declaration itself.
The real question is not whether the same keyword must be used for all "user-defined type" statements, the question is whether the class or struct keyword can be used to give "internal declaration of type" a certain commitment.
The strategically Correct struct (The Politically Correct Struct) The skill of C Programmers sometimes becomes a trap for C ++ programmers, such as placing an array of a single unit at The end of a struct, therefore, each struct objects can have an array of variable sizes:
Struct mumber {/* stuff */char pc [1];}; // obtain a string from the archive or standard input device // then configure enough memory for the struct itself and the string struct mumble * pmumbl = (struct mumble *) malloc (sizeof (struct mumble) + strlen (string) + 1); strcpy (& memble. pc, string );
If the class is used for declaration, and the class is:
Multiple accesskeys, including data
Derived from another class
Define one or more virtual functions
It may be a smooth conversion, maybe not.
In C ++, all data in the same access section must appear in the memory layout in the declarative order. However, if the data is placed in multiple access sections, the order is not certain.
In the same way, the data member layout of base classes and derived classes is not mandatory. The existence of vitual function also makes the validity of the previous conversion a question mark.
If you need a very complex part of the data of C ++ class to make it look like a C Declaration, it is best to extract that part into an independent struct statement, the function of combining C and C ++ is to derive the C ++ part from C struct:
struct C_Point {...};class Point : public C_point {...}
Therefore, both C and C ++ can be supported:
extern void draw_line(Point, Point);draw_line(Point(0, 0), Point(100, 100));draw_rect(Point(0, 0), Point(100, 100));
This practice is no longer recommended because some compilers have made some changes to the class inheritance layout in the mechanism that supports virtual functions,
Composition, rather than inheritance, is the only feasible method to combine C and C ++.(The conversion operator provides a very compiled extraction method ):
struct C_point {...};class Point {public: operator C_point() { return _c_point; }private: C_point _c_point;};
A rational use of C struct in C ++ is to transfer "all or part of a complex class object" to a C function, the struct statement can encapsulate data and ensure a space layout compatible with C. However, this guarantee only exists in composition. If it is "inheritance" rather than "Combination", the compiler determines whether additional data members should be arranged in base struct subobject.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.