一 微软C++标准关键字(包括标准C++,托管C++,微软扩展C++,C++/CLI)
注释:
0) 一般关键字前面加双下划线的表示微软的扩展,也有没有加的啊
1) 关键字后面有1的表示:__declspec 关键字的扩展属性
2) 关键字后面有2的表示:是以前的托管C++的扩展
3) 关键字后面有3的表示:是事件处理中使用的属性,在非托管C++中也可以使用
4)VS6.0只支持非托管C++,VS2002,VS2003还支持托管C++,最新的VS2005中还增加了对C++/CLI的支持。
5)VS IDE中通过
/Za 可以控制使用微软扩展或只使用标准C++.
二 几个常用的微软扩展关键字实例
1)在非托管C++中使用__raise
,__event
// EventHandlingRef_raise.cpp
struct E {
__event void func1();
void func1(int) {}
void func2() {}
void b() {
__raise func1();
__raise func1(1); // C3745: 'int Event::bar(int)':
// only an event can be 'raised'
__raise func2(); // C3745
}
};
int main() {
E e;
__raise e.func1();
__raise e.func1(1); // C3745
__raise e.func2(); // C3745
}
2)在非托管C++中使用
__hook ,
__unhook // evh_native.cpp
#include <stdio.h>
[event_source(native)]
class CSource {
public:
__event void MyEvent(int nValue);
};
[event_receiver(native)]
class CReceiver {
public:
void MyHandler1(int nValue) {
printf_s("MyHandler1 was called with value %d.\n", nValue);
}
void MyHandler2(int nValue) {
printf_s("MyHandler2 was called with value %d.\n", nValue);
}
void hookEvent(CSource* pSource) {
__hook(&CSource::MyEvent, pSource, &CReceiver::MyHandler1);
__hook(&CSource::MyEvent, pSource, &CReceiver::MyHandler2);
}
void unhookEvent(CSource* pSource) {
__unhook(&CSource::MyEvent, pSource, &CReceiver::MyHandler1);
__unhook(&CSource::MyEvent, pSource, &CReceiver::MyHandler2);
}
};
int main() {
CSource source;
CReceiver receiver;
receiver.hookEvent(&source);
__raise source.MyEvent(123);
receiver.unhookEvent(&source);
}
3)在托管C++中使用__Delegate(VS2005中使用/CLR:: oldsytax)
// keyword__delegate.cpp
// compile with: /clr
#using <mscorlib.dll>
using namespace System;
__delegate int GetDayOfWeek();
__gc class MyCalendar
{
public:
MyCalendar() : m_nDayOfWeek(4) {}
int MyGetDayOfWeek() { Console::WriteLine("handler"); return m_nDayOfWeek; }
static int MyStaticGetDayOfWeek() { Console::WriteLine("static handler"); return 6; }
private:
int m_nDayOfWeek;
};
int main ()
{
GetDayOfWeek * pGetDayOfWeek; // declare delegate type
int nDayOfWeek;
// bind delegate to static method
pGetDayOfWeek = new GetDayOfWeek(0, &MyCalendar::MyStaticGetDayOfWeek);
nDayOfWeek = pGetDayOfWeek->Invoke();
Console::WriteLine(nDayOfWeek);
// bind delegate to instance method
MyCalendar * pcal = new MyCalendar();
pGetDayOfWeek = static_cast<GetDayOfWeek*>(Delegate::Combine(pGetDayOfWeek,
new GetDayOfWeek(pcal, &MyCalendar::MyGetDayOfWeek)));
nDayOfWeek = pGetDayOfWeek->Invoke();
Console::WriteLine(nDayOfWeek);
// delegate now bound to two methods; remove instance method
pGetDayOfWeek = static_cast<GetDayOfWeek*>(Delegate::Remove(pGetDayOfWeek,
new GetDayOfWeek(pcal, &MyCalendar::MyGetDayOfWeek)));
return 0;
} 4)在C++/CLI中使用Event,Delegate (VS2005 IDE 中使用/CLR)
// mcppv2_events.cpp
// compile with: /clr
using namespace System;
// declare delegates
delegate void ClickEventHandler(int, double);
delegate void DblClickEventHandler(String^);
// class that defines events
ref class EventSource {
public:
event ClickEventHandler^ OnClick; // declare the event OnClick
event DblClickEventHandler^ OnDblClick; // declare OnDblClick
void FireEvents() {
// raises events
OnClick(7, 3.14159);
OnDblClick("Hello");
}
};
// class that defines methods that will called when event occurs
ref class EventReceiver {
public:
void OnMyClick(int i, double d) {
Console::WriteLine("OnClick: {0}, {1}", i, d);
}
void OnMyDblClick(String^ str) {
Console::WriteLine("OnDblClick: {0}", str);
}
};
int main() {
EventSource ^ MyEventSource = gcnew EventSource();
EventReceiver^ MyEventReceiver = gcnew EventReceiver();
// hook handler to event
MyEventSource->OnClick += gcnew ClickEventHandler(MyEventReceiver, &EventReceiver::OnMyClick);
MyEventSource->OnDblClick += gcnew DblClickEventHandler(MyEventReceiver, &EventReceiver::OnMyDblClick);
// invoke events
MyEventSource->FireEvents();
// unhook handler to event
MyEventSource->OnClick -= gcnew ClickEventHandler(MyEventReceiver, &EventReceiver::OnMyClick);
MyEventSource->OnDblClick -= gcnew DblClickEventHandler(MyEventReceiver, &EventReceiver::OnMyDblClick);
}
三 在VS2002,VS2003中我们可以使用托管C++(Managed C++),但是最新的VS2005中,我们可以直接在C++中使用.NET FrameWork ,这个就是最新的C++标准C++/CLI,已经通过了相关组织的标准化。