Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions contained in the C++ class

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "C++ class contains functions", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, let's study and learn what C++ class contains functions!

The C++ class contains three functions that we care about: the constructor, the destructor, and all the important DoSomething functions. We need to wrap each function into its equivalent C++ function and share it with you here.

/ / original class class CFoo {public: CFoo (int x); ~ CFoo (); int DoSomething (int y);}; / / flattened C code void* _ stdcall new_CFoo (int x) {return new CFoo (x);} int _ stdcall CFoo_DoSomething (void* handle, int y) {CFoo * foo = reinterpret_cast (handle); return foo- > DoSomething (y) } void _ stdcall delete_CFoo (void * handle) {CFoo * foo = reinterpret_cast (handle); delete foo;}

There are several important points to pay attention to here. First, notice that each C++ class is mapped to a simple C function. Second, we observe that we explicitly use the _ _ stdcall calling habit for the C function. In the previous DLL article, we knew that it was troublesome to simply call the unformatted C function in MSVC DLL.

If we give up going through all kinds of hardships to use it, we can make this effort a little easier. The easiest way to get Borland to call Microsoft DLL is for DLL to export the unformatted, undecorated, _ _ stdcall function that calls the customary C++. Borland and Microsoft have different handling of the _ _ cdecl function.

Usually, they also treat the _ _ stdcall function differently because MSVC modifies the _ _ stdcall function, but we can prevent this behavior by adding a DEF file to the MSVC project. See the download section for an example of an DEF file. The other thing to note about the code is that the new_CFoo function returns a pointer to the CFoo object. The BCB caller must save the pointer locally. This may seem to contradict the theme of this article.

After all, I don't think BCB can use C++ classes from MSVC DLL? If that's true, why do we return a pointer to the CFoo object? The answer is that BCB cannot call member functions of the MSVC DLL export class. However, this does not mean that it cannot store the address of such an object. New_CFoo returns a pointer to a CFoo object.

The BCB client can store this pointer, but cannot use it. BCB cannot discard it (you should not try to do so). To make this point a little easier to understand, new_CFoo returns a null pointer (it doesn't return anything else anyway). On the BCB side, there is no safe way to handle this null pointer except to store it and pass it back to DLL.

Ok, before we move on, there are two other things to pay close attention to. First, notice that CFoo_DoSomething takes a null pointer as its * parameter, which is the same null pointer that new_CFoo returns. Null pointers are traced back to CFoo objects with reinterpret_cast (you know, when you see a reinterpret_cast.

You are dealing with ugly code). The DoSomething member function is called after the conversion. * Note that a null pointer is also a parameter of the C++ class. Wrapping DLL to delete objects is crucial. You should not call delete on a null pointer in BCB. Obviously it won't do what you want.

Thank you for your reading, the above is the "C++ class contains functions" content, after the study of this article, I believe you have a deeper understanding of the C++ class contains functions, the specific use of the need for everyone to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report