uefi编程多半是以C语言进行开发,要想使用类和模板要在cpp文件中以如下方式包含头文件
#ifdef __cplusplus
extern "C"{
#endif
#include <uefi.h>
#include <library/uefilib.h>
#include <library/basememorylib.h>
#include <Library/MemoryAllocationLib.h>
#include <library/uefiapplicationentrypoint.h>
#include <Library/UefiBootServicesTableLib.h>
#include <library/baselib.h>
#include <library/printlib.h>
//#include <typeinfo>
#ifdef __cplusplus
}
#endif
并定义NULL
#undef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((VOID*)0)
#endif
然后为了消除bool类型的警告要在inf文件中的[BuildOptions] 模块中加上
MSFT:*_*_*_CC_FLAGS = /wd4804
然后就可以写如下代码
class TestClass
{
public:
TestClass(){Print((CHAR16*)L"hello world c plus plus \r\n");}
~TestClass(){Print((CHAR16*)L"bye bye !!!");}
};
//typeid操作符的返回结果是名为type_info的标准库类型的对象的引用
template<typename type1, typename type2>
void TestTemplate(type1 a, type2 b)
{
Print((CHAR16*)L"size of first arg is %d size of second arg is %d \r\n", sizeof(a), sizeof(b));
}
EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE *SystemTable)
{
CHAR16 Msg[100];
UnicodeSPrint(Msg, 100, (CHAR16*)L"hello world \r\n");
Print(Msg);
TestClass t;
UINT32 a = 1;
CHAR16 *b = NULL;
TestTemplate(a, b);
return EFI_SUCCESS;
}
这样个还不支持new和delete操作。。