Suggestions for the use of C ++ Builder and hosting C ++ properties

zhaozj2021-02-16  38

C Builder and Host C Properties General Suggestions: 1. Any member variables of the class correspond to the corresponding properties. The external unacceptable member variables use protected or private properties. 2. Any member variables of the class are tried to be accessed by attribute. No matter It is a member variable within the class to access its own member variables or a member variable inside the class external access class. 3. In addition to the function, constructive function, destructor, the destructor, etc., the internal access to its own member variables, try to use attributes 4. Attributes as much as possible for simple data types or simple classes. Types of properties are the most simple data type or simple class. 5.c Builder: Complex type properties, pointer or object Application. Host C : Complex type properties, replace it with functions. It is recommended to use GET and SET heads to correspond to the attribute read and write. Note: This sometimes we can easily ignore, because I can easily forget the default copy in C Composition function, when the parameter is a complex object, there is a similar problem. Interpretation of Article 5 in C Builder: Detailed source code See example project

//SampleClassUnit.h file // ------------------------------------------ ---------------------------------

#ifndef sampleclassunith # define sampleclassunith // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ---------------------------------

#include

/ / -------------------------------------------------------------------------------------------- --------------------------- Class Tbasic {private: // user declarationspublic: // user declarations int m_inum; int * m_pnum; public: // user declarations __fastcall tbasic (); __fastcall ~ tbasic ();

/ / -------------------------------------------------------------------------------------------- --------------------------- Class Theritor {private: // user declarations tbasic __fastcall getbasicobject ();

TBASIC * __FASTCALL getBasicPointer ();

TBASIC & __FASTCALL GetBasicReference (); public: // user declarations tbasic m_basic

Tbasic * m_pbasic;

TBasic m_BasicReference; public: // User declarations __fastcall THeritor (); __fastcall ~ THeritor (); public: __property TBasic BasicObject = {read = GetBasicObject}; __property TBasic * BasicPointer = {read = GetBasicPointer}; __property TBasic & BasicReference = {read = GetBasicReference};

/ / -------------------------------------------------------------------------------------------- ---------------------------

/ / -------------------------------------------------------------------------------------------- --------------------------- # Endif // SampleClassUnit.cpp file // ------------- -------------------------------------------------- ----------------

#pragma HDRSTOP

#include "sampleclassunit.h"

#pragma package (smart_init)

/ / -------------------------------------------------------------------------------------------- ---------------------------__ fastcall tbasic :: tbasic () {m_inum = 10;

m_pnum = new int (100);

__fastcall tbasic :: ~ tbasic () {delete m_pnum; m_pnum = NULL;

/ / -------------------------------------------------------------------------------------------- ---------------------------__ fastcall theritor :: theritor () {m_pbasic = new tbasic ();

__fastcall theitor :: ~ theritor () {delete m_pbasic; m_pbasic = null;

/ / -------------------------------------------------------------------------------------------- --------------------------- TBASIC __FASTCALL THERITOR :: getBasicobject () {return m_basic;}

TBASIC * __FASTCALL THERITOR :: getBasicPointer () {return m_pbasic;}

TBASIC & __FASTCALL THERITOR :: getBasicReference () {return m_basicreference;} // ---------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------

// Test function void __fastcall tsampleform :: Button1click (Tobject * sender) {// Pointer Properties If (m_pheritor! = Null) {tbasic * pbasic = m_pheritor-> BasicPointer; int inum = PBASIC-> M_INUM; Richedit1-> Lines- > Add ("m_inum =" INTOSTR (Inum)); if (pbasic-> m_pnum! = Null) {int pnum = * (PBASIC-> M_PNUM); Richedit1-> Lines-> add ("m_pnum =" INTSTR (iPnum));} else {richedit1-> lines-> add ("m_pnum = null!");}}} // --------------------- -------------------------------------------------- ---- void __fastcall tsampleform :: Button2Click (Tobject * sender) {// Reference attribute if (m_pheritor! = null) {tbasic * pbasic = & (m_pheritor-> baricreference); int inum = pbasic-> m_inum; richedit1- > Lines-> add ("m_inum =" INTOSTR (Inum)); if (PBASIC-> m_pnum! = Null) {int ipnum = * (PBASIC-> m_pnum); richedit1-> lines-> add ("m_pnum = " INTSTR (IPNUM));} else { Richedit1-> lines-> add ("m_pnum = null!");}}} // ----------------------------- ----------------------------------------------

Void __fastcall tsampleform :: Button3Click (Tobject * sender) {// Object property if (m_pheritor! = null) {tbasic * pbasic = & (m_pheritor-> BasicObject); int inum = PBASIC-> m_inum; richedit1-> lines-> Add ("M_INUM =" INTOSTR (Inum); if (PBASIC-> m_pnum! = Null) {INT IPNUM = * (PBASIC-> M_PNUM); Richedit1-> Lines-> add ("m_pnum =" INTSTR });} else {richedit1-> lines-> add ("m_pnum = null!");}}} // ---------------------- -------------------------------------------------- --- void __fastcall tsampleform :: Button4click (Tobject * sender) {// Pointer IF (m_pheritor! = null) {tbasic * pbasic = m_pheritor-> m_pbasic; int inum = pbasic-> m_inum; richedit1-> lines-> add ("m_inum =" INTOSTR (Inum)); if (PBASIC-> m_pnum! = null) {int ipnum = * (PBASIC-> m_pnum); Richedit1-> Lines-> add ("m_pnum =" INTOSTR (IPNUM ));} Else {rich Edit1-> Lines-> Add ("m_pnum = null!");}}} // ----------------------------- ----------------------------------------------

void __fastcall TSampleForm :: Button5Click (TObject * Sender) {// reference if (m_pHeritor = NULL!) {TBasic * pBasic = & (m_pHeritor-> m_BasicReference); int iNum = pBasic-> m_iNum; RichEdit1-> Lines-> Add ("m_inum =" INTOSTR (Inum)); if (PBASIC-> m_pnum! = null) {int ipnum = * (PBASIC-> m_pnum); Richedit1-> Lines-> add ("m_pnum =" INTOSTR (IPNUM ));} Else {richedit1-> lines-> add ("m_pnum = null!");}}} // ----------------------- -------------------------------------------------- --void __fastcall tsampleform :: Button6click (Tobject * sender) {// object if (m_pheritor! = null) {tbasic * pbasic = & (m_pheritor-> m_basic); int inum = pbasic-> m_inum; richedit1-> Lines- > Add ("m_inum =" INTOSTR (Inum)); if (pbasic-> m_pnum! = Null) {int pnum = * (PBASIC-> M_PNUM); Richedit1-> Lines-> add ("m_pnum =" INTSTR (ipnum);} else { Richedit1-> lines-> add ("m_pnum = null!");}}} // ----------------------------- ----------------------------------------------

Host C : When your class is used in WebService, when the WebService client requests an object of such an object to the server, the server will format the relevant data of the class into the client, including all The value of the attribute. If the property includes complex type, it may cause objects in the properties, and the objects are in the object, so that the nesting is very deep, the server formatted object data is a long time,

1. It is best not to return a complex object. If the property is obtained is a custom object, or some objects with a pointer. This situation is best to return the object of the pointer or object of the object. Because Returns the properties of the object, call the object's copy constructor, and the system overhead is large, and the second is to call your own destructive function when the copy of the object obtained through the property is prevented when it exceeds its scope. The object analysis of the pointer refers to.

转载请注明原文地址:https://www.9cbs.com/read-26212.html

New Post(0)