(1) C ++ does not allow calling another constructor (called a Delegate constructor) in a constructor, whereas C # does. For example: C ++: Struct Point { Public: Int X, Y; Point (int x, int y ); Point (Point pt): Point (pt. X, pt. Y) {} // error, C ++ not allowed }; C #: Struct Point { Public int X, Y; Public Point (int x, int y ); Public Point (Point pt): Point (pt. X, pt. Y) {} // Yes, C # Allow }; The syntax of delegate constructors is very natural and easy to understand. Therefore, you may question whether C ++ does not provide it to programmers. In fact, C ++ does not provide this feature because of syntax, but resource management (Oh, there are many other things for C ++ ). We know that the constructor of C ++ is used to allocate resources, while the constructor is used to release resources. The constructor and the constructor call must match, otherwise, the basic rules of C ++ are broken. If Delegate constructor calls are allowed, the constructor is executed twice, and the Destructor is executed only once. Of course, for some classes, such as the previous Point, this is not a problem, but from the perspective of language mechanisms, this feature may be a "dangerous" feature. Note: In the latest draft C ++ standard proposal, Herb and others have a proposal on allowing delegate constructors to call, of course, this is largely to facilitate C ++/CLI binding. (2) In the C ++ constructor, virtual function calls are automatically converted into common function calls by the compiler, and virtual function calls are allowed in the C # constructor. In C ++, there is a natural reason for this processing-in C ++, the object is initialized after the constructor is executed. For multi-State objects, this means that the constructor executes a very important thing behind the scenes-initializing the virtual function table of the object. If we call a virtual function in the base class constructor, the virtual function table of the object is still a virtual function table of the base class, so the correct virtual function call cannot be performed. This is the reason. Generally, we should avoid calling the virtual function in the constructor because it violates the semantics of the virtual function. In C #, the object type information has been initialized before the object's constructor is executed, so normal virtual function calls can be performed. |