码迷,mamicode.com
首页 > 编程语言 > 详细

CLI/C++中混合类的使用【转】

时间:2016-06-22 15:25:04      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:

http://www.cppblog.com/mzty/archive/2007/12/24/39517.html

CLI/C++中混合类的使用


一 混合类

所谓混合类是指CLI/C++中native的Class中可以包含CLR对象,CLR的class也可以包含Naitve的对象。

1)native的class中包含CLR对象,必须通过gcroot<>或auto_gcroot<>。
2)CLR中的class中包含native的对象,必须是指针,也可以使用高手写的CAutoNativePtr智能指针。


注意:C#中不能调用CLI/C++中的Native的class。同样Native C++中也不能调用CLI/C++中的Ref的class。

二 实例

 高手的CAutoNativePtr类:


技术分享    Author    :    Nishant Sivakumar
技术分享    Email    :    voidnish@gmail.com    
技术分享    Blog    :    http://blog.voidnish.com
技术分享    Web        :    http://www.voidnish.com     
技术分享
技术分享    You may freely use this class as long as you include
技术分享    this copyright. 
技术分享    
技术分享    You may freely modify and use this class as long
技术分享    as you include this copyright in your modified version. 
技术分享
技术分享    This code is provided "as is" without express or implied warranty. 
技术分享    
技术分享    Copyright ?Nishant Sivakumar, 2006.
技术分享    All Rights Reserved.
技术分享***/
技术分享
技术分享#pragma once
技术分享
技术分享template<typename T> ref class CAutoNativePtr
技术分享{
技术分享private:
技术分享    T* _ptr;
技术分享
技术分享public:
技术分享    CAutoNativePtr() : _ptr(nullptr)
技术分享    {
技术分享    }
技术分享
技术分享    CAutoNativePtr(T* t) : _ptr(t)
技术分享    {
技术分享    }
技术分享
技术分享    CAutoNativePtr(CAutoNativePtr<T>% an) : _ptr(an.Detach())
技术分享    {
技术分享    }
技术分享
技术分享    template<typename TDERIVED> 
技术分享        CAutoNativePtr(CAutoNativePtr<TDERIVED>% an) : _ptr(an.Detach())
技术分享    {
技术分享    }
技术分享
技术分享    !CAutoNativePtr()
技术分享    {    
技术分享        delete _ptr;
技术分享    }
技术分享
技术分享    ~CAutoNativePtr()
技术分享    {
技术分享        this->!CAutoNativePtr();
技术分享    }
技术分享
技术分享    CAutoNativePtr<T>% operator=(T* t)
技术分享    {
技术分享        Attach(t);
技术分享        return *this;
技术分享    }
技术分享
技术分享    CAutoNativePtr<T>% operator=(CAutoNativePtr<T>% an)
技术分享    {
技术分享        if(this != %an)
技术分享            Attach(an.Detach());
技术分享        return *this;
技术分享    }
技术分享
技术分享    template<typename TDERIVED> 
技术分享        CAutoNativePtr<T>% operator=(CAutoNativePtr<TDERIVED>% an)
技术分享    {
技术分享        Attach(an.Detach());
技术分享        return *this;
技术分享    }
技术分享
技术分享    static T* operator->(CAutoNativePtr<T>% an)
技术分享    {
技术分享        return an._ptr;
技术分享    }
技术分享
技术分享    static operator T*(CAutoNativePtr<T>% an)
技术分享    {
技术分享        return an._ptr;
技术分享    }
技术分享
技术分享    T* Detach()
技术分享    {
技术分享        T* t = _ptr;
技术分享        _ptr = nullptr;
技术分享        return t;
技术分享    }
技术分享
技术分享    void Attach(T* t)
技术分享    {
技术分享        if(t)
技术分享        {    
技术分享            if(_ptr != t)
技术分享            {
技术分享                delete _ptr;
技术分享                _ptr = t;
技术分享            }
技术分享        }
技术分享        else        {#ifdef _DEBUG            throw gcnew Exception(                "Attempting to Attach() a nullptr!");#endif        }            }    void Destroy()    {        delete _ptr;        _ptr = nullptr;    }};

测试实例之CLI/C++文件:

技术分享
技术分享#pragma once
技术分享#include <string>
技术分享#include <iostream>
技术分享#include <gcroot.h>
技术分享#include <msclr/auto_gcroot.h>
技术分享
技术分享#include "AutoNative.h"
技术分享
技术分享using namespace System;
技术分享
技术分享namespace MixedNativeAndCLIDLL {
技术分享
技术分享    public class NativeClass
技术分享    {
技术分享    public:
技术分享        int *pX;    
技术分享        NativeClass(){pX = new int(10);}
技术分享        ~NativeClass()
技术分享        {
技术分享            if(pX != NULL)
技术分享            {
技术分享                delete pX;
技术分享                pX = NULL;
技术分享            }
技术分享        }        
技术分享    };
技术分享
技术分享    public ref class RefClass
技术分享    {
技术分享    public:
技术分享        int x;    
技术分享        RefClass(){x = 20;}
技术分享    };
技术分享
技术分享    public class MixedClass0
技术分享    {
技术分享        public:
技术分享            NativeClass nativeClass;
技术分享            //RefClass refClass; // error c3265 and error c3149
技术分享            gcroot<RefClass^> refClass1;
技术分享
技术分享            std::string nativeStr;
技术分享            //System::String refStr; // error c3265 and error c3149
技术分享            gcroot<System::String^> refStr1;
技术分享
技术分享            MixedClass0()
技术分享            {
技术分享                refClass1 = gcnew RefClass();
技术分享                refStr1 = gcnew System::String("i am a native class mixed some clr members.\n");
技术分享            }
技术分享            ~MixedClass0()
技术分享            {            
技术分享                delete refClass1;
技术分享                delete refStr1;
技术分享            }
技术分享
技术分享            void PrintSelf()
技术分享            {
技术分享                System::Console::WriteLine("my name is MixedClass0");
技术分享                System::Console::WriteLine(refClass1->x);
技术分享                System::Console::WriteLine(refStr1);
技术分享            }
技术分享    };
技术分享
技术分享    public class MixedClass1
技术分享    {
技术分享        public:
技术分享            NativeClass nativeClass;
技术分享            //RefClass refClass; // error c3265 and error c3149
技术分享            msclr::auto_gcroot<RefClass^> refClass1;
技术分享
技术分享            std::string nativeStr;
技术分享            //System::String refStr; // error c3265 and error c3149
技术分享            msclr::auto_gcroot<System::String^> refStr1;
技术分享
技术分享            MixedClass1()
技术分享            {
技术分享                refClass1 = gcnew RefClass();
技术分享                refStr1 = gcnew System::String("i am a native class with some clr members.\n");
技术分享            }
技术分享            ~MixedClass1()
技术分享            {
技术分享                // no need to delete.            }                    void PrintSelf()            {                System::Console::WriteLine("my name is MixedClass1");                System::Console::WriteLine(refClass1->x);                System::Console::WriteLine(refStr1);            }    };    public ref class MixedClass2    {        public:            //NativeClass nativeClass; // error c4368            NativeClass * nativeClass1;            RefClass^ refClass;                         //std::string nativeStr; // error c4368            std::string *nativeStr1;            System::String^ refStr; //                 MixedClass2()            {                nativeClass1 = new NativeClass();                nativeStr1 = new std::string("i am a clr class with some native members.\n");            }            ~MixedClass2()            {                delete nativeClass1;                delete nativeStr1;            }            !MixedClass2(){}            void PrintSelf()            {                System::Console::WriteLine("my name is MixedClass2");                std::cout<<*(nativeClass1->pX)<<std::endl;                std::cout<<*nativeStr1<<std::endl;                            }    };        public ref class MixedClass3    {        public:            //NativeClass nativeClass; // error c4368            CAutoNativePtr<NativeClass> nativeClass1;            RefClass^ refClass;                         //std::string nativeStr; // error c4368            CAutoNativePtr<std::string> nativeStr1;            System::String^ refStr; //                 MixedClass3()            {                nativeClass1 = new NativeClass();                nativeStr1 = new std::string("i am a clr class with some native members.\n");            }            ~MixedClass3(){}            !MixedClass3(){}            void PrintSelf()            {                System::Console::WriteLine("my name is MixedClass3");                std::cout<<*(nativeClass1->pX)<<std::endl;                std::cout<<*nativeStr1<<std::endl;                            }    };}

测试实例之C#调用文件:
using System.Collections.Generic;
技术分享using System.Text;
技术分享
技术分享namespace CsharpTest
技术分享{
技术分享    class Program
技术分享    {
技术分享        static void Main(string[] args)
技术分享        {
技术分享            MixedNativeAndCLIDLL.MixedClass0 mixedClass0 = new MixedNativeAndCLIDLL.MixedClass0();
技术分享            //mixedClass0.PrintSelf();
技术分享            MixedNativeAndCLIDLL.MixedClass1 mixedClass1 = new MixedNativeAndCLIDLL.MixedClass1();
技术分享            //mixedClass1.PrintSelf();
技术分享            MixedNativeAndCLIDLL.MixedClass2 mixedClass2 = new MixedNativeAndCLIDLL.MixedClass2();
技术分享            mixedClass2.PrintSelf();
技术分享            MixedNativeAndCLIDLL.MixedClass3 mixedClass3 = new MixedNativeAndCLIDLL.MixedClass3();
技术分享            mixedClass3.PrintSelf();
技术分享        }
技术分享    }
技术分享}
技术分享

三 代码下载

http://www.cppblog.com/Files/mzty/MixedNativeAndCLITest.rar

 

CLI/C++中混合类的使用【转】

标签:

原文地址:http://www.cnblogs.com/mazhenyu/p/5607062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!