Archive for ‘之语言特性’ Category

July 6, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
//~ #include <string>
 
using namespace std;
 
class Base
{
public:
	virtual void foo()
	{
		cout<<"Base::foo()"<<endl;
	}
};
 
class Derived: public Base
{
private:
	void foo()
	{
		cout<<"Derived::foo()"<<endl;
	}
};
 
int 
main()
{
	Derived  * pD = new Derived;
	Base * pB = pD;
	pB->foo();				//~ OK, call Derived::foo()
	//~ pD->foo();				//~ oops, Derived::foo() is private!
	return 0;
}

可见,权限检查发生在名字查找之时,而虚函数表的访问不带有权限检查。

Tags: ,. 9 views
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
//~ #include <sstream>
using namespace std;
 
int main()
{
	typedef vector<int> int_vector;
	typedef istream_iterator<int> istream_itr;
	typedef ostream_iterator<int> ostream_itr;
	typedef back_insert_iterator<vector<int> > back_ins_itr;
	//~ vector
	int_vector ivec;
	//~ 从标准输入读整数到vector容器
	copy(istream_itr(cin), istream_itr(), back_ins_itr(ivec));
	//~ 排序
	sort(ivec.begin(), ivec.end());
	//~ 输出到标准输出
	copy(ivec.begin(), ivec.end(), ostream_itr(cout, "\n"));
	return 0;
}
Tags: ,. 9 views
1
2
3
4
5
//~ 判断字符c是否为空白符(空格、水平制表、垂直制表、换页、回车和换行符)
int my_isspace( int ch )
{
    return (unsigned int)(ch - 9) < 5u  ||  ch == ' ';
}
Tags: . 14 views
July 5, 2009
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
 
int
main()
{
	Derived * pD = new Derived;
	pD->foo();
	return 0;
}
Tags: ,. 32 views
July 1, 2009

函数调用时,调用者依次把参数压栈,然后调用函数,函数被调用以后,在堆栈中取得数据,并进行计算。函数计算结束以后,或者调用者、或者函数本身修改堆栈,使堆栈恢复原装。 在参数传递过程中需要解决两个问题:

  • 当参数大于一个时,参数的入栈顺序如何,即:从右向左亦或是由右向左。
  • 恢复堆栈的任务是由调用函数完成,还是被调用者负责。
Tags: ,,. 37 views
June 28, 2009

windows中的visual studio固然强大,但是对于平时测试用的小程序来说,建立一个工程毕竟是麻烦的。相对来说用g++在命令行下进行编译就方便多了(当然也可以用cl.exe),找到了这么一个工具,cygwin-b20,比较小巧实用。从这里下载
安装及使用方法:
直接使用可执行文件full.exe进行安装,安装目录建议选择默认路径。安装完成后,将路径C:\cygnus\cygwin-b20\H-i586-cygwin32\bin加入的系统环境变量PATH中,这样,你可以在任何目录下执行bin/的命令,其中包含了169个较为常用的linux命令

Tags: ,,,. 975 views
1
2
3
4
5
6
7
8
9
class quick_sort
{
public:
	typedef int T;
	explicit quick_sort(T* src, int beg, int end);
private:
	void sub_sort(int beg, int end);
	T* m_src;
};
Tags: ,. 39 views
June 27, 2009

boost目前的最新版本是1.39,下载地址:
http://sourceforge.net/project/showfiles.php?group_id=7586&package_id=8041

下载后,解压boost_1_39_0.tar.gz

1
$ tar -zxvf boost_1_39_0.tar.gz

然后进入解压缩后的文件夹编译boost的编译器jam

1
2
$ cd boost_1_39_0\tools\jam
$ ./build_dist.sh
Tags: ,. 21 views
June 26, 2009
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iomanip>
//~ #include <sqlite.h>
//~ #include <mysql/mysql.h>
using namespace std;
template <unsigned int x, unsigned int y, bool is_ordered = (x >= y)> //~ 一般情况下x>=y
struct static_gcd 
{ 
    static int const value = static_gcd<y, x % y>::value; 
};
Tags: ,. 12 views

class D
这里面有三张函数表,分别对应d的三个基类。事实上,d的虚函数d()的地址保存在了基类B1对应的虚函数表内。从多重继承的内存布局,我们可以看到子类新加入的虚函数被加到了第一个基类的虚函数表,所以当dynamic_cast的时候,子类和第一个基类的地址相同,不需要移动指针,但是当dynamic_cast到其他的父类的时候,需要做相应的指针的移动。

Tags: ,. 109 views
Page 6 of 9123456789