2007年9月23日

  哈哈,从M$ Visual C++ Team的Andy Rich那里又偷学到一招:VC8的隐含编译项/d1reportSingleClassLayout/d1reportAllClassLayout 。看个复杂的例子吧(如下),现在假设我们想知道Derived类的对象布局,怎么办? 在Project Properties->C++->Command Line->Additional Options里面加上/d1reportSingleClassLayoutDerived吧!

class CommonBase
{
    
int co;
};

class Base1: virtual public CommonBase
{
public:
    
virtual void print1() {}
    
virtual void print2() {}
private:
    
int b1;
};

class Base2: virtual public CommonBase
{
public:
    
virtual void dump1() {}
    
virtual void dump2() {}
private:
    
int b2;
};

class Derived: public Base1, public Base2
{
public:
    
void print2() {}
    
void dump2() {}
private:
    
int d;
};

int _tmain(int argc, _TCHAR* argv[])
{
    
return 0;
}

F5编译之,你会惊奇地发现,Output里面有如下字样:

 1 class Derived size(32):
 2    +---
 3    | +--- (base class Base1)
 4  0 | | {vfptr}
 5  4 | | {vbptr}
 6  8 | | b1
 7    | +---
 8    | +--- (base class Base2)
 9 12 | | {vfptr}
10 16 | | {vbptr}
11 20 | | b2
12    | +---
13 24 | d
14    +---
15    +--- (virtual base CommonBase)
16 28 | co
17    +---
18 
19 Derived::$vftable@Base1@:
20  0 | &Base1::print1
21  1 | &Derived::print2
22 
23 Derived::$vftable@Base2@:
24  0 | &Base2::dump1
25  1 | &Derived::dump2
26 
27 Derived::$vbtable@Base1@:
28  0 | -4
29  1 | 24 (Derivedd(Base1+4)CommonBase)
30 
31 Derived::$vbtable@Base2@:
32  0 | -4
33  1 | 12 (Derivedd(Base2+4)CommonBase)
34 
35 Derived::print2 this adjustor: 0
36 Derived::dump2 this adjustor: 12

看到了吗? VC8居然输出了Derived对象的完整布局! 我们终于可以不必两眼一抹黑般的去peek/poke了....第1行表明,Derived对象总占用了32字节;其由三部分组成,分别是行3-行7、行 8-行12、行13、行28;其中前二者分别是基类Base1、Base2的布局,最后的行28为虚拟基类Common的布局。

以基类 Base1部分为例,可发现其由一个虚函数表指针vftable和虚基表指针vbtable构成,先看Base1部分的vftable所指向的虚表$ vftable@Base1(行19),不难发现,其中的表项2已经被Derived::print2给override了;再来看Base2部分的 vftable所指向的虚表$vftable@Base2(行23),可发现,同样的,Base2::dump2被Derived::dump2给 override了。这不明摆着就是虚函数机制嘛,heh~

值得注意的是,这个例子同时说明,多继承场合下,其实在单一对象中是存在多个 this指针的....行35-36给出了如何将Derived的this指针校正为其基类子对象this指针的偏移量,也就是说,根据行36,假设有个 Derived d,那么d.dump1()实际上应该理解成通过虚表$vftable@Base2对((Base2*)(((char*)&d)+12))- >dump1()的调用....即传递给所有Base2成员函数的this指针应该是(Base2*)((char*)(&d)+12),这里可能我写得恐怖了点,意思到了就成....这不,普通继承、多继承、对象Slicing的语义都在这个布局里面了,看仔细了哈~

OK,多继承看完了,继续看虚拟基类是如何布局的。虚基Common在Derived的布局中,位于Derived本身数据成员之后的位置。Base1、 Base2中均保存了一个vbtable指针,其分别指向各自所使用的虚基表$vbtable@Base1和$vbtable@Base2,为什么要指向一个虚基表? 很简单,因为Base1、Base2有可能会同时继承多个不同的虚拟基类.....这充分体现了C++对象布局的复杂性....在每个虚基表中,保存了所继承的虚拟基类部分相对于子类部分vbtable指针的偏移值,以Base2为例,我们知道Base2的vbtable在Derived中的偏移值为16 (行10),则根据$vbtable@Base2,虚基Common部分距离Base2 vbtable指针的偏移值为12,则有虚基Common在Derived中的总偏移值为16+12。与普通多继承同理,我们在调用非虚拟的虚基成员函数时,必须将Derived的this指针调整为指向虚基部分的this指针,只有这样才能成功地访问虚基自身的数据成员和虚基的虚拟函数(通过虚基自己的 vftable,为简单起见,上例中我就没弄那么复杂了,大家可以自己玩玩,明白如何举一反三即可)
posted @ 2007-12-07 09:56 daiyie 阅读(635) | 评论 (2)编辑 收藏

Transferring the Password

There are various ways to get the passwords stored in KeePass into other windows. The first, and most simple method is copying them to the clipboard. For this, just double-click onto the specific field in the main password list. Example: if you want to copy the password of entry X, point onto the password field of the entry in the main view and double-click. The password is copied to the clipboard. If you enable the auto-clearing option, KeePass will clear the clipboard automatically after some seconds. This prevents you from forgetting to clear the clipboard yourself, leaving sensitive data in the clipboard.

The second method is drag-n-drop. As in method 1, point onto the field you want to use, click the left mouse button and hold it. Drag the data into other windows.

The third, and the most powerful method is auto-type. KeePass features a very mighty auto-type feature, which types user names, passwords, etc. into other windows for you. The default auto-typing sequence is: {USERNAME}{TAB}{PASSWORD}{TAB}{ENTER}. But this sequence is customizable, per entry (read the CHM documentation file that comes with KeePass for more about this). This makes the auto-type feature applicable to all windows and webforms you'll ever see. There are two submethods to perform an auto-type:

  • Selecting an entry: Just select the entry that you want to get auto-typed, right-click onto it and click "Perform Auto-Type". KeePass will minimize itself, the window that had the focus before will come to the front. KeePass starts typing the data into this window.
  • Global hot key: This is the most powerful of all methods. You leave KeePass running in the background. As soon as you're on a site that requires a login (the password of which you stored in KeePass before), just press a hot key (by default, Ctrl-Alt-A). KeePass immediately auto-types the data into the target window.
posted @ 2007-10-25 21:31 daiyie 阅读(393) | 评论 (0)编辑 收藏

#include <boost/config.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <vector>
#include <algorithm>

template<class T>
inline T & ToRef( T *pT)
{
 return *pT;
}
#define TODEF(x) bind( ToRef<x>, _1)

template<class T>
inline void DeletePointer( T *pT)
{
 delete pT;
}
#define ALL(v) (v).begin(), (v).end()

struct X
{
public:
 int m_t;
 X():m_t(11)
 {
 }
 
    bool f(int a)
 {
  std::cout << m_t << std::endl;
  return true;
 }
};

int main()
{
    using namespace boost;
 using namespace std;
 
 vector<X *> v;
 for( int i=0; i<10; ++i)
  v.push_back( new X);
 
 for_each( ALL(v), bind(&X::f, TODEF(X), 4 ) );
 
 for_each( ALL(v), bind( DeletePointer<X>, _1) );
 
 return 0;
}

 

posted @ 2007-09-23 17:09 daiyie 阅读(217) | 评论 (1)编辑 收藏

导航

<2007年9月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

统计

常用链接

留言簿(1)

随笔档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜