Windows Mobile下native c++ typeid的使用

來源:互聯網
上載者:User
背景

我在開發Mobile Sensors API - Native unified APIs for Windows Mobile Sensors Unit Test的過程中,想把Sensor對象的類列印出來,所以需要使用typeid來實現。

簡介

本文講述在Windows Mobile下如何使用c++的typeid操作符。

實現
IGSensor* GSensorFactory::CreateGSensor()
{
try
{
return HTCGSensor::Create();
}
catch(std::runtime_error& err)
{
printf("%s\n", err.what());
}

try
{
return SamsungGSensor::GetInstance();
}
catch(std::runtime_error& err)
{
printf("%s\n", err.what());
}
return NULL;
}

上面是Sensor工廠類,理想狀況下,程式可以自動檢測裝置類型,產生相應的Sensor處理類。

所以我在Unit Test的時候,可以把運行時的類資訊列印出來,方便測試。

TEST(IGSensor, GSensorTest )
{
IGSensor* gSensor = GSensorFactory::CreateGSensor();

CHECK(gSensor != NULL);
FAIL(typeid(*gSensor).name());
}

gSensor 定義為父類IGSensor,但是GSensorFactory返回的是其子類HTCGSensor或者SamsungGSensor。typeid可以把運行時的對象的類型資訊列印出來。

下面是用wikipedia的例子簡單解釋一下typeid。原文見Typeid。

#include <iostream>
#include <typeinfo> //for 'typeid' to work

class Person {
public:
// ... Person members ...
virtual ~Person() {}
};

class Employee : public Person {
// ... Employee members ...
};

int main () {
Person person;
Employee employee;
Person *ptr = &employee;

std::cout << typeid(person).name() << std::endl; // Person (statically known at compile-time)
std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compile-time)
std::cout << typeid(ptr).name() << std::endl; // Person * (statically known at compile-time)
std::cout << typeid(*ptr).name() << std::endl; // Employee (looked up dynamically at run-time
// because it is the dereference of a pointer to a polymorphic class)
}

typeid的功能是在啟動並執行時候判斷(determine)對象的類資訊。其返回type_info的對象,但是type_info的建構函式,==操作符都是私人的,所以不可能產生新的type_info的對象,每次使用都類似typeid(person).name() ,直接返回類資訊。

雖然說typeid的功能是運行時判斷,但是有寫情況下,編譯器是編譯時間判斷的,例如上述例子中的前三個如下:

std::cout << typeid(person).name() << std::endl;   // Person (statically known at compile-time)
std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compile-time)
std::cout << typeid(ptr).name() << std::endl; // Person * (statically known at compile-time)

這些都是編譯時間判斷的。只有多態對象的指標才會運行時判斷。如下:

std::cout << typeid(*ptr).name() << std::endl;     // Employee (looked up dynamically at run-time
// because it is the dereference of a pointer to a polymorphic class)

 

在我的程式中IGSensor的指標gSensor也使用了運行時判斷。在Visual Studio下開發Windows Mobile,需要注意的是如果使用了運行時判斷,需要修改項目編譯選項。Configuration Properties -> C/C++ -> Language -> Enable Run-Time Type Info -> Yes。否則程式運行時會出錯。

 

關於Mobile Sensors API項目

這個項目還是在起步階段,當前實現了samsung的重力感應器,我把項目host到 Mobile Sensors API - Native unified APIs for Windows Mobile Sensors 了,我會持續改進,把各種sensors的實現到這個項目中。

由於我手頭上沒有HTC的機器,如果誰有興趣可以加入到項目中幫我測試HTC裝置,由於加入了Unit Test,測試變得很簡單,只需要執行程式,參考測試輸出檔案就可以了,不需要調試。當然這個測試過程是一個不斷迭代的過程,只是Unit Test把子過程簡單化了。

原始碼:http://mobilesensor.codeplex.com/SourceControl/ListDownloadableCommits.aspx

環境:VS2008 + WM 6 professional SDK + Samsung Windows Mobile SDK

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.