2008年7月3日星期四

类属指针 void *

类属指针可以隐式的转换成人任意类型的指针
void * malloc(size_t)
char * pch = malloc(10);

2008年7月2日星期三

point to member function 成员函数指针

class CA
{
public:
CA(void (*pfn)(int));
};

class CB
{
public:
CB()
{
CA a(&Fx1);
};
public:
void Fx1(int)
{};
};
/////////////
error C2276: '&' : illegal operation on bound member function expression

solution:

class CA;

class CB
{
public:
CB(void (CA::*)()){};
};


class CA
{
public:
CA()
{
CB b(&CA::Fx);
};
void Fx();
};

2008年6月30日星期一

virtual inherit

class B
{
void Fb(){...};
};
class CA : virtual public B
{
void Fca(){...};
};
class CB : virtual public B
{
void Fcb(){...};
};

class CS: public CA, public CB
{
Fb();
};

B
----
CA
----
CB
----
CS




Err:
class B
{
public:
void Fb()
{
printf("Fbfffffffffffffffffffffffffffffffffffffffffffffffff");
}
};
class CA : public B
{


};
class CB : public B
{

};

class CS: public CA, public CB
{
public:
void Do()
{
Fb();
}
};

int _tmain(int argc, _TCHAR* argv[])
{
CS c;
c.Do();
return 0;
}
//////////////////////////////////////////////////////
1>f:\project\rreg\tt\tt.cpp(28) : error C2385: ambiguous access of 'Fb'
1> could be the 'Fb' in base 'B'
1> or could be the 'Fb' in base 'B'
1>f:\project\rreg\tt\tt.cpp(28) : error C3861: 'Fb': identifier not found

[COM]-ruler of ref count

1. Addref before return. Call Addref if function return interface point.(QueryInterface, CreateInstance)
2. Call "Release" after unuse interface.
3. Call "AddRef" after copy a interface point


HRESULT hr = pIUnknown->QueryInterface(IID_IX, (void **)&pIX)
if (SUCCEEDED(hr))
{
pIX->Fx();
pIX->Release(); //Release if succeeded
}
pIUnknown->Release();


equal:

HRESULT hr = pIUnknown->QueryInterface(IID_IX, (void **)&pIX)
pIUnknown->Release();
if (SUCCEEDED(hr))
{
pIX->Fx();
pIX->Release(); //Release if succeeded
}

for 3:

IX *pIX2 = pIX; //Make a copy of pIX
pIX2->AddRef();
...
pIX2->Release();
pIX->Release();

[转]static_cast、dynamic_cast、reinterpret_cast、和const_cast

static_cast

用法:static_cast <> ( expression )

该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法:
  • 用于类层次结构中基类和子类之间指针或引用的转换。进行上行转换(把子类的指针或引用转换成基类表示)是安全的;进行下行转换(把基类指针或引用转换成子类表示)时,由于没有动态类型检查,所以是不安全的。
  • 用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。这种转换的安全性也要开发人员来保证。
  • 把空指针转换成目标类型的空指针。
  • 把任何类型的表达式转换成void类型。
注意:static_cast不能转换掉expression的const、volitale、或者__unaligned属性。

dynamic_cast

用法:dynamic_cast <> ( expression )

该 运算符把expression转换成type-id类型的对象。Type-id必须是类的指针、类的引用或者void *;如果type-id是类指针类型,那么expression也必须是一个指针,如果type-id是一个引用,那么expression也必须是一个 引用。

dynamic_cast主要用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。

在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。
class B{

public:

int m_iNum;

virtual void foo();

};

class D:public B{

public:

char *m_szName[100];

};



void func(B *pb){

D *pd1 = static_cast(pb);

D *pd2 = dynamic_cast(pb);

}

在 上面的代码段中,如果pb指向一个D类型的对象,pd1和pd2是一样的,并且对这两个指针执行D类型的任何操作都是安全的;但是,如果pb指向的是一个 B类型的对象,那么pd1将是一个指向该对象的指针,对它进行D类型的操作将是不安全的(如访问m_szName),而pd2将是一个空指针。另外要注 意:B要有虚函数,否则会编译出错;static_cast则没有这个限制。这是由于运行时类型检查需要运行时类型信息,而这个信息存储在类的虚函数表 (关于虚函数表的概念,详细可见)中,只有定义了虚函数的类才有虚函数表,没有定义虚函数的类是没有虚函数表的。

另外,dynamic_cast还支持交叉转换(cross cast)。如下代码所示。
class A{

public:

int m_iNum;

virtual void f(){}

};



class B:public A{

};



class D:public A{

};



void foo(){

B *pb = new B;

pb->m_iNum = 100;

D *pd1 = static_cast(pb); //copile error

D *pd2 = dynamic_cast(pb); //pd2 is NULL

delete pb;

}

在函数foo中,使用static_cast进行转换是不被允许的,将在编译时出错;而使用 dynamic_cast的转换则是允许的,结果是空指针。

reinpreter_cast

用法:reinpreter_cast (expression)

type-id必须是一个指针、引用、算术类型、函数指针或者成员指针。它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针(先把一个指针转换成一个整数,在把该整数转换成原类型的指针,还可以得到原先的指针值)。

该运算符的用法比较多。

const_cast

用法:const_cast (expression)

该运算符用来修改类型的const或volatile属性。除了const 或volatile修饰之外, type_id和expression的类型是一样的。

常量指针被转化成非常量指针,并且仍然指向原来的对象;常量引用被转换成非常量引用,并且仍然指向原来的对象;常量对象被转换成非常量对象。

Voiatile和const类试。举如下一例:
class B{

public:

int m_iNum;

}

void foo(){

const B b1;

b1.m_iNum = 100; //comile error

B b2 = const_cast(b1);

b2. m_iNum = 200; //fine
}

上面的代码编译时会报错,因为b1是一个常量对象,不能对它进行改变;使用const_cast把它转换成一个常量对象,就可以对它的数据成员任意改变。注意:b1和b2是两个不同的对象。

2008年5月21日星期三

Ensuring Only One Instance of Your Application Is Running -- 确保只有一个程序实例运行

Many applications must ensure that they have only one instance running. There are several ways to do this in Windows XP. Among them are the following:
许多程序都要确保只能运行一个实例。这个有多个实现方法。总的有以下:

→Use FindWindow or FindWindowEx to search for a known window that your application opens. If that window is already open, you can use that as an indication that the application is already running.
使用FindWindow或者FindWindowEx去查找你的应用程序窗口,如果窗口打开了,就说明程序已经在运行。

→Create a mutex or semaphore object when your application is opened, and close that object when the application terminates. The global object namespace is separated for each desktop, allowing a unique list of mutex and semaphore objects for each.
当你的应用程序打开时创建一个互斥或者信号对象,在程序结束时关闭这个对象。这个全局的对象名字空间在每个桌面是分开的,每个桌面允许有一系统的唯一的互斥和信号对象。

[Reference from MSDN-User Accounts with Fast User Switching and Remote DesktopUser Accounts with Fast User Switching and Remote Desktop]

2008年5月15日星期四

Out-of-band / 带外数据

1. A person is involved in a discussion with a group of people. During the discussion, he decides that some information is best kept between himself and one other member of the group, exclusively -- whether for privacy reasons, or merely the need to avoid saturating the entire group with information they do not need. His communication with that individual may be considered out-of-band, relative to the group discussion.

1. 一个人加入到一个讨论组中。在这个讨论期间,他决定有些信息最好对这个组的其他人保密。无论是私人的原因或者是仅仅是为了避免这些对这个组无用的信息。他的这个与个别人的这种交流就可以看成是out-of-band(相对于这个讨论组)

2. A person is at his place of work, and he is communicating with someone outside of his work place (for example, by Internet or telephone, using his employer's computer or telephone equipment). He decides that key parts of his conversation are best kept private from his employer, such as if he were discussing plans to change jobs. He would use his personal cell phone to transmit sensitive messages to, and receive related messages from, his external contact, in order to avoid use of his employer's phone or computer network. In this situation, his personal cell phone communications are considered out-of-band, relative to the main conversation.

2. 一个人在他的工作场所,他与外面的人交流(如打电话).他觉得有些谈话内容最好不要让老板知道,如想跳槽。他可以用他的手机来发送,接收敏感的信息,而不通过老板电话或计算机网络。这种情况下,手机就可以称做是out-of-band了,相对于这个主要的谈话。

[From wikipedia]