什么是静态类?
静态类不用生成类实例即可用来访问其功能函数,当我们不需要成员数据或根据对象变化的行为,就可以使用这样的静态类。
如何在Symbian中生成静态类?
我们不需要描述类时加上static前缀,只要在所有的函数前面加上static前缀即可。你不需要描述和实现构造及析构函数,因为根本不需要对象实体
头文件
// MyStatic.h.
/** No Need to derive from CBase as this class will never be instantiated over heap**/
class MyStaticClass
{
public: //all function must be public else no one can able to access it.
static void OpenOtherApplication(const TDesC& aUrl);
static HBufC8* ReadFromFile(RFs& aFs, const TDesC& aRootPath, const TDesC& aFileName);
static TInt StartExe(const TDesC& aUID);
private:
/** Make Desctructor Pure Virtual so that the class cannot be instantiated or inherited**/
virtual ~MyStaticClass()=0;
};
源文件
//MyStatic.cpp implementation
void MyStaticClass::OpenOtherApplication(const TDesC& aAppName)
{
//do your work here.
}
HBufC8* MyStaticClass::ReadFromFile(RFs& aFs, const TDesC& aRootPath, const TDesC& aFileName)
{
//do your work here.
}
TBool MyStaticClass::StartExe(const TDesC& aUID)
{
//do your work here.
}
如何使用静态类?
Include MyStatic.h in your class. then directly use as follows
TInt error = MyStaticClass::StartExe(0xea3e3e10); //no need to create instance here.