Difference in the syntax between Delphi and C ++

Source: Internet
Author: User
1. Delphi can never create an object on the stack

The following is a common Delphi code that declares all the local variables required in this process at the beginning of the process:

Procedure Foo;
VaR
OBJ: tobject; // This sentence is easily misunderstood by C ++ programmers.
Begin
...
End;

C ++ programmers will think that the OBJ variable is the tobject object instance itself. They will think that this variable declares and constructs an object instance of the tobject class on the stack, they will be confused with the following C ++ code:

Void Foo () {cobject OBJ;//This sentence indeed constructs the cobject class on the stack
...//An object instance, which is automatically parsed when the foo function is left.
}

Keep in mind that in Delphi, it is never possible to construct an object on the stack, nor to pass values on an object. What programmers can see is "pointer to an object instance ", to put it simply, "Everything is a pointer". In the above example, obj is actually a "tobject type Pointer". When it is declared on the stack, its value is unknown (like C ++), and no object is constructed. The above code is translated into C ++, basically:

Void Foo (){
Cobject * OBJ;//Declare a pointer of the cobject type
//But no object is constructed or associated with it
...
}

In Delphi, sizeof (tobject), sizeof (Self), and sizeof (OBJ) are all 4, that is, the size of a 32-bit pointer.

2. Delphi constructor is more like a class method (static member function)

Since Delphi does not allow the construction of objects on the stack, the object instance can only be created on the stack. Delphi does not have the New Keyword (there is a procedure named new ), instead, the object is constructed using a syntax different from C ++ (on the stack:

Procedure Foo;
VaR
OBJ: tobject; // obj is essentially a pointer of the tobject type.
Begin
OBJ: = tobject. Create; // construct a tobject object instance on the heap and assign its address to OBJ.
OBJ. Free; // structure of the object pointed to by OBJ
End;

Like C ++, functions constructed on the stack will not be automatically destructed when they leave the scope, so some of them will leave the foo process, call the Free Method of tobject to describe it. The free method calls the destroy destructor, but before calling destroy, it determines whether self is null. If it is null, it will be returned directly. The self in Delphi is the this in C ++.

Delphi is a single inheritance, and all classes are derived from tobject. The constructor of all classes must be named create, and the Destructor must be named destroy. Of course, the Destructor must be a virtual function.

From the declaration form, the create method is like a member function, but in terms of usage, it is more like a class method (static member function in C ++ ), because when calling it, the qualified name is not an object, but a class name (txxxxx. create ).

Iii. Pure virtual methods can be called in the destructor of Delphi

In destroy, the destroy destructor in Delphi can call any pure virtual function (which is unimaginable in C ++, the virtual method table must not be damaged. Therefore, if the base class determines the function to be called during structure analysis, even if the function is a virtual function or even a pure virtual function.

Iv. Delphi automatically clears member variables during construction

When a class in any Delphi is constructed, all its member variables are cleared, the boolean type is initially false, the string is initially empty, and the integer and floating point types are initialized to 0 ...... C ++ has no such guarantee

5. If an exception is thrown in the Delphi constructor, The Destructor is automatically called first.

In Delphi, if an exception is thrown in the constructor, The Destructor is automatically executed first, and then the exception is thrown out. In C ++, if an exception is thrown in the constructor, The Destructor will not be called.

6. Delphi simplifies addref, release, and QueryInterface in the COM interface

C ++ typically uses templates to encapsulate com interfaces. in Delphi, addref, release, and QueryInterface are all hidden by the compiler, when a variable of the iunknown type (essentially a pointer) is assigned to another variable, the compiler automatically addref behind it, when an iunknown variable leaves the scope (no one uses it anymore), the release is automatically called, and the QueryInterface is abstracted as the as operator:

Procedure Foo (const aparam: iunknown );
VaR
Bar: iunknown;
Other: istream;
Begin
Bar: = aparam; // The instance to which aparam points is addref once because of the value assignment operation.
Other: = bar as istream; // the QueryInterface is called once, and the reference count is added again.
End; // when returned, both other and bar leave the scope and are called each time for release.

Using templates (such as _ com_ptr) in C ++ can also automate reference counting, but QueryInterface is not that convenient.

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.