c++/cli(四)——クラス&構造体

2497 ワード

目次
クラス#クラス#
c++のクラス
管理クラス
コードの説明
キーワードref
非管理クラスポインタ
コンストラクタ
こうぞうかんすう
メンバー関数
こうぞうたい
次は開発段階に入ります.
クラス#クラス#
クラスの処理は,c++のクラス外でさらに1層カプセル化することであり,ここではc++におけるクラスの実現が何であるか,c++クラスにおけるプライベートメンバーが何であるかに関心を持たず,使用する方法を1層カプセル化するだけでよい.
c++のクラス
#include 
#include 

class Person
{
    std::string m_name;
    unsigned m_age;

public:
    Person()
    ~Person()

public:
    void show()
    {
        std::cout<

管理クラス
public ref class PersonCli
{
    Person* m_pobj;

public:
    PersonCli() : m_pobj(new Person) {}
    !PersonCli() 
    {
        if (m_pobj)
        {
            delete m_pobj;
            m_pobj = nullptr;
        }
    }
    
public:
    void show()
    {
        m_obj->show();
    }
};

コードの説明
キーワードref
c++/cliでは、クラスはrefクラスとvalueクラスに分けられます.ここでは、次の文を参照します.
value class  is a  ValueType  - that means, whenever you assign it to another variable of the same type, the whole object gets copied into the other variable, leaving you with two separate copies. Examples of this are basic numeric data types like  intbool  or  doubleValueTypes  are sealed, which means you cannot derive from them.
ref class  is a reference type - if you assign it to another variable of the same type, you copy only a reference. So the two variables basically "point"to the same data.
So the main difference between  value class  and  ref class  are the copying semantics. Both can contain Methods, fields properties and so on. Also, you cannot derive from a  value class .
非管理クラスポインタ
管理クラスには、管理クラス以外のオブジェクトは存在しませんが、そのポインタは存在します.
コンストラクタ
管理クラスの構造関数では、newは非管理クラスのポインタを出し、その後、非管理クラス関数の呼び出しに対して、このポインタに依存する.
こうぞうかんすう
管理クラスでは、構造関数の書き方が非管理クラスの構造関数とは異なります.「!」を使用します.「~」ではなく.その理由については、以下を参照してください.https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ms177197(v=vs.100)?redirectedfrom=MSDN
メンバー関数
c++dllの再カプセル化であるため、管理クラスのメンバー関数は非管理クラスのメンバー関数名と一致することができ、もちろん異なることも可能であり、再呼び出しの際には、非管理クラスのポインタを用いて呼び出す.
 
こうぞうたい
構造体はクラスと似ていますが、classキーワードをstructキーワードに置き換えるだけです.
ただし、使用する場合、構造体のデータメンバーは管理データメンバーであるべきであるため、非管理c++を呼び出すときに、構造体変換を行う必要があります.すなわち、非管理構造体を新規作成し、管理構造体のすべての変数値を非管理構造体に割り当て、パラメータを渡す必要があります.