当前位置: 代码迷 >> Symbian >> Helloworld程序详解,该怎么解决
  详细解决方案

Helloworld程序详解,该怎么解决

热度:6437   发布时间:2013-02-26 00:00:00.0
Helloworld程序详解
[size=16px][/size]Hello World!First step to Symbian


S60 SDK的HelloWorldBasic例子@C:\Symbian\9.2\S60_3rd_FP1\S60Ex\HelloWorldBasic\group\bld.inf

详细文档@C:\Symbian\9.2\S60_3rd_FP1\S60Ex\HelloWorldBasic\doc

导入C:\Symbian\9.2\S60_3rd_FP1\S60Ex\HelloWorldBasic\group\bld.inf进Carbide C++

详细来看下Symbian的应用程序启动

1. EXE文件自E32Main()入口(在helloworldbasic.cpp中)


E32Main()调用NewApplication()创建一个HelloWorldApplication类实例,并返回其指针。Symbian随后使用该指针完成application的创建。

/**
* A normal Symbian OS executable provides an E32Main() function which is
* called by the operating system to start the program.
*/
GLDEF_C TInt E32Main()
{
return EikStart::RunApplication( NewApplication );
}


在同文件(helloworldbasic.cpp)中可以看到CHelloWorldApplication的创建

/**
* factory function to create the Hello World Basic application class
*/
LOCAL_C CApaApplication* NewApplication()
{
return new CHelloWorldBasicApplication;
}

下面跟踪一下Symbian的具体系统实现:

在eikstart.h里面来仔细看一下EikStart::RunApplication()

class EikStart
/**
This is the API to initialise and run the application architecture for applications built as exes.
It is not intended to be used for generally running applications from other code. Use RApaLsSession for that.

@publishedAll
@released
*/
{
public:
IMPORT_C static TInt RunApplication(TApaApplicationFactory aApplicationFactory);
private:
static void RunApplication(const TApaApplicationFactory& aApplicationFactory,CApaCommandLine* aCommandLine);
};

#endif

定义里面有一个工厂类TApaApplicationFactory,该类可以看作是建立应用程序的工厂。

在E32Main()函数中EikStart::RunApplication( NewApplication ),参数却是指向NewApplication函数的指针。

到apparc.h中去看TApaApplicationFactory类的定义:

class TApaApplicationFactory
/** Encapsulates the functionality of creating an application, whether it be via a factory function
or an ECOM plugin. Instances of this class can usually be created implicitly when required as
function parameters - just specify the function pointer, ECOM plugin UID or CImplementationInformation
reference.

@publishedAll
@released
@see CApaProcess
@see EikStart */
{
public:
typedef CApaApplication* (*TFunction)();
public:
IMPORT_C TApaApplicationFactory();
IMPORT_C TApaApplicationFactory(TFunction aFunction);
IMPORT_C TApaApplicationFactory(const CImplementationInformation& aEmbeddedApplicationInformation);
IMPORT_C TApaApplicationFactory(TUid aEmbeddedApplicationUid);
CApaApplication* CreateApplicationL() const;
HBufC* AppFileNameL() const;
TUid AppFileUid() const;
private:
enum TType
  {
  ETypeFunction, // if iType is this, iData is a TFunction
  ETypeEmbeddedApplicationInformation, // if iType is this, iData is an ECOM CImplementationInformation
  ETypeEmbeddedApplicationUid // if iType is this, iData is an ECOM implementation TUid
  };
private:
static CApaApplication* CreateEmbeddedApplicationL(TUid aUid);
static HBufC* EmbeddedApplicationDisplayNameLC(TUid aUid);
static HBufC* FullAppFileNameL(const TDesC& aAppName);
static void CleanupImplementationArray(TAny* aImplementationArray);
private:
TType iType;
TUint iData;
mutable CApaApplication* iApplication; // used to be iSpare1
TInt iSpare2;
};

调用EikStart::RunApplication过程中,编译器创建一个TApaApplicationFactory对象,其构造函数的参数是NewApplication的函数指针。

这里面有隐式转换,可以查一下C++primer 

we saw that a nonexplicit constructor that can be called with one argument defines an implicit conversion.the complier will use that conversion when an object of the argument type is suupplied and an object of the class type is needed .

可以以一个实参调用的非explicit构造函数(可能有默认参数)定义了一种隐式转换。
当我们需要该类类型对象时,而如果我们只提供了该类构造函数的形参类型的实参,这种转换就发生。
  相关解决方案