C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

Managed c++与c++\CLI的区别(实例)

1 Managed c++ (使用vs2002,2003,建立visual c++下的console application(.net ) project,project还有各种类型)
   注: 此时的project setting里一定是 :using Managed Extenstions ->Yes

简单实例

#using <mscorlib.dll>
using namespace System;

__gc 
class G 
{
public:
    
int k;
    
int sum(int);
}
;

G::sum(
int i) {return i*(i + 1)/2;}

int main()
{
    G 
* g = new G;
    Console::WriteLine(g
->sum(4)); // 结果输出10
    return 0;
}
 
接口实例

#using <mscorlib.dll>
using namespace System;

__gc __interface Ibase1 
{
    
int f(int);
}
;
__gc __interface Ibase2 
{
    
int f(int);
}
;
__gc 
struct C: Ibase1, Ibase2 {
    
int f(int i) // 接口方法的实现
        return 2*i-1;
    }
;
}
;

int main(void){
    C
* c = new C;
    Console::WriteLine((c 
-> f(1)).ToString()); // 输出结果为1
    Console::WriteLine((__try_cast (c)->f(2)).ToString()); 
    
// 输出结果为3

    Console::WriteLine((__try_cast (c)
->f(3)).ToString());
    
// 输出结果为5

    
return 0;
}

property实例:

#using <mscorlib.dll>
using namespace System;

__gc 
class G {
public:
    __property 
int get_Size() {
        Console::WriteLine(S
"get_属性");
        
return nSize;
    }
;
    __property 
void set_Size(int i) 
        Console::WriteLine(S
"set_属性");
        nSize 
= i;
    }
;
private:
    
int nSize;
}
;

int main() {
    G 
* pG = new G;
    pG
->Size = 10// 调用set_Size
    int i = pG->Size; // 调用get_Size
    Console::WriteLine(i);
}
 

  
//程序结果为:

     
//    set_属性

        
//    get_属性

        
//       10

代理实例:
#using <mscorlib.dll>
using namespace System;

__delegate 
int GetDayOfWeek(); // 委派方法的声明
__gc class MyCalendar
{
public:
    MyCalendar() : m_nDayOfWeek(
4{}
    
int MyGetDayOfWeek() {
        Console::WriteLine(
"非静态方法"); 
        
return m_nDayOfWeek; 
    }

    
static int MyStaticGetDayOfWeek() 
        Console::WriteLine(
"静态方法"); 
        
return 6
    }

private:
    
int m_nDayOfWeek;
}
;

int main(void)
{
    GetDayOfWeek 
* pGetDayOfWeek; // 声明委派类型变量
    int nDayOfWeek;

    
// 将类的静态方法MyStaticGetDayOfWeek绑定成委派
    pGetDayOfWeek = new GetDayOfWeek(0&MyCalendar::MyStaticGetDayOfWeek);
    nDayOfWeek 
= pGetDayOfWeek->Invoke(); // 委派的调用
    Console::WriteLine(nDayOfWeek);

    
// 将一个类的实例绑定成委派
    MyCalendar * pcal = new MyCalendar();
    pGetDayOfWeek 
= 
        static_cast(Delegate::Combine(pGetDayOfWeek,
        
new GetDayOfWeek(pcal, &MyCalendar::MyGetDayOfWeek)));
    nDayOfWeek 
= pGetDayOfWeek->Invoke();
    Console::WriteLine(nDayOfWeek);

    
// 删除绑定委派的类实例
    pGetDayOfWeek = 
        static_cast(Delegate::Remove(pGetDayOfWeek,
        
new GetDayOfWeek(pcal, &MyCalendar::MyGetDayOfWeek)));

    
return 0;
}
 

 
/* 输出结果是:

         静态方法
           
            6

               静态方法

                  非静态方法

                     4
*/


2 c++\CLI (使用vs2005 ,之前的版本不支持,建立visual c++下的CLR Console Application ,当然还有其他的类型可选,比如CLR empty ...^_^,)
  注:此时project setting 中common language runtime support : Common Language Runtime Support (/clr) ,此时的project中已经ref system....DLL,所以不用想上面那样#using <mscordll>

简单实例

#include "stdafx.h"
using namespace System;

int main(array<System::String ^> ^args)
{
    String
^ str = gcnew String("Hello World");
    Object
^ o1 = gcnew Object();
    Console::WriteLine(str);
    
return 0;
}

interface 实例

using namespace System;

interface class IDog
{
    
void Bark();
}
;

ref class Dog : IDog
{
public:
    
void Bark()
    
{
        Console::WriteLine(
"Bow wow wow");
    }

}
;

void _tmain()
{
    Dog
^ d = gcnew Dog();
    d
->Bark();
}


property实例

ref class Base
{
public:
    
ref struct SBaseData
    
{
    
public:
        SBaseData()
        
{
            x 
= 10;
        }

        SBaseData(
int n)
        
{
            x 
= n;
        }

        
int x;        
    }
;
private:
    SBaseData
^ _bd;
public:
    Base()
    
{
        _bd 
= gcnew SBaseData();
    }

    
virtual property SBaseData^ Data
    
{
        SBaseData
^ get()
        
{
            
return _bd;
        }

        
void set(SBaseData^ val)
        
{
            _bd
->= val->x;
        }

    }

}
;
ref class Derived : Base
    
{
    
public:
        
ref struct SDerivedData : Base::SBaseData
        
{
        
public:
            SDerivedData(
int n1, int n2) : SBaseData(n1)
            
{
                y 
= n2;
            }

            SDerivedData() : SBaseData()
            
{
                y 
= 100;
            }

            
int y;        
        }
;
    
private:
        SDerivedData
^ _bd;
    
public:
        Derived()
        
{
            _bd 
= gcnew SDerivedData();
        }


        
virtual property SBaseData^ Data
        
{
            SBaseData
^ get() override //= Base::Data::get
            {
                
return _bd;
            }

            
void set(SBaseData^ val) override //= Base::Data::set
            {
                
try
                
{
                    _bd
->= val->x;
                    _bd
->= safe_cast<SDerivedData^>(val)->y;
                }

                
catch(System::InvalidCastException ^)
                
{
                    
//log an error
                }

            }

        }

        
virtual property SDerivedData^ SData
        
{
            SDerivedData
^ get()  
            
{
                
return _bd;
            }

            
void set(SDerivedData^ val) 
            
{
            
                _bd
->= val->x;
                _bd
->= val->y;
                            
            }

        }

    }
;
int main(array<System::String ^> ^args)

{
    Derived
^ d = gcnew Derived;
    
//Base^ d = gcnew Derived;   // error , y is not a member of Base class
    int num = d->Data->x;
    
int num2 = d->SData->y;
    System::Console::WriteLine(num);
    System::Console::WriteLine(num2);
    
return 0;
}


index实例

using namespace System;
using namespace System::Collections;
ref class R
{
private:
    Hashtable
^ h;
public:
    R()
    
{
        h 
= gcnew Hashtable();
    }


    
//Named property
    property int Age[String^]
    
{
    
protected:
        
int get(String^ s)
        
{
            
if(h->ContainsKey(s))
            
{
                
for each(DictionaryEntry de in h)
                
{
                    
if(s->CompareTo(de.Key) == 0)
                        
return (int)de.Value;
                }

            }

            
return 0;
        }
    

        
void set(String^ s, int age)
        
{
            h
->Add(s,age);
        }

    }


    
//Default property
    property int default[String^]
    
{
        
int get(String^ s)
        
{
            
return Age[s];
        }


        
void set(String^ s, int age)
        
{
            Age[s] 
= age;
        }

    }
    
}
;

int main(array<System::String ^> ^args)

{
    R
^ r = gcnew R();

    r[
"Nish"= 27;
    r[
"Smitha"= 15;
    r[
"Chris"= 21;
    r[
"Don"= 87;

    Console::WriteLine(r[
"Nish"]);
    Console::WriteLine(r[
"George"]);
    Console::WriteLine(r[
"Smitha"]);
    Console::WriteLine(r[
"Don"]);
    Console::WriteLine(r[
"Bert"]);
    
return 0;

posted on 2006-08-18 10:58 梦在天涯 阅读(3302) 评论(0)  编辑 收藏 引用 所属分类: CPlusPlusManage c++ /CLI


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1748249
  • 排名 - 5

最新评论

阅读排行榜