Differences between C ++ and C # Constructor

Source: Internet
Author: User

(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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.