C++ class:List x 与 List x() 的巨大差别

2010年4月10日 16:44 Slyar 没有评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

假设List是一个C++类的名字,问: List x 和 List x() 有区别么?

A big difference!

函数f()声明了一个名为x的List类的实例

void f()
{
List x;     // Local object named x (of class List)
...
}

而函数g()声明的是一个名为x()的函数,它的返回值是一个List

void g()
{
List x();   // Function named x (that returns a List)
...
}

注意,这个和构造函数不一样(new List & new List())...

分类: 编程相关 标签: ,

关于构造/析构函数与主函数调用顺序的疑惑

2010年3月28日 10:41 Slyar 6 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

这学期开了C++课,讲到构造函数和析构函数的时候我产生了一个疑问,老师说要学了操作系统才能明白,暂时先记录一下,以后回来解决。

很早以前就知道main()是程序执行的入口,一直理解的都是操作系统从main()开始的地方执行程序,但是C++又说析构函数在程序执行之后才被自动调用,我的问题就是程序最后的"return 0"和析构函数谁先执行?还有就是全局实例构造函数的调用顺序又是怎么样的?

为了先得到结论,我用VS2008写了一段程序来实验这个问题。

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
#include "stdafx.h"
#include <iostream>
#include <string>
 
using namespace std;
 
class Demo
{
public:
	string objName;
	Demo(string Name)
	{
		this->objName = Name;
		cout << objName << " Constructor called" << endl;
	}
	~Demo()
	{
		cout << objName << "Destructor called" << endl;
	}
};
 
Demo obj1("obj1");
 
int main()
{
	cout << "Main() start" << endl;
	Demo obj2("obj2");
	return int(&(cout << "Main() end" << endl));
}

最后的结果很奇怪,很奇怪...

obj1 Constructor called
Main() start
obj2 Constructor called
Main() end
obj2Destructor called
obj1Destructor called

1、全局实例的构造函数在main()之前被调用

2、析构函数都在main()之后被调用

悲剧了,第1个构造函数和最后2个析构函数居然不是main()来调用,那它们到底是谁调用的?显然在main()执行之前程序进行了一些列操作,至少包括全局变量的生成(因为全局变量是定义在堆上的)和main()参数的传递...暂时无视这个问题,欲知后事如何,请看下回分解...

分类: 编程相关 标签:

分治算法求N个数中第K小(大)的数

2010年3月26日 14:56 Slyar 2 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

恩,这个学期开算法课,跟着进度写写代码就好。这周讲分治,说到了求N个数中第K小(大)数的问题,写写看。

分治算法的复杂度是O(n),用到了快速排序的思路:先选取一个参考数进行一次快排,拿升序来说的话,快排之后左边所有数<参考数,右边所有数>参考数,然后根据左右部分各自包含元素的个数,计算要求的元素在哪个区间内(要求的元素或是左区间中的第k小数,或是右区间中第[k-j]小数,j为左区间的长度),然后递归求解。

本来pku 2104和pku 2761都是求第K小数的题,无奈用这个算法TLE,只能用后面的划分树或是其他复杂度更低的算法来做,以后再说吧。我这里只贴用这个算法做的,虽然交上去会TLE...

阅读全文...

分类: 编程相关 标签: ,

CentOS 5 彻底关闭SELinux

2010年3月23日 14:38 Slyar 1 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

最近写东西的时候发现这个玩意太讨厌了,总是冒出一些无聊的错误让我浪费笔墨,索性直接禁用它,以后写安全方面的时候再说...

用vim打开 /etc/selinux/config

在 SELINUX=enforcing 前面加个#号注释掉它
#SELINUX=enforcing

然后新加一行
SELINUX=disabled

保存,退出,重启系统,搞定。

分类: Linux相关 标签:

Install VMware Tools on CentOS(Linux)

2010年3月19日 12:17 Slyar 2 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

1、Make sure you already have gcc and kernel devel installed. You can install it by running the command.

yum install gcc kernel-devel

2、Select "Tools" from the VMWare Workstation GUI menu, and then "Install VMWare Tools". This doesn't actually appear to do anything in the guest machine. All it really does is to connect the virtual CD-ROM device to the appropriate CD image containing the tools for your virtual machine.

3、Open a console or SSH session.

4、Mount the virtual cd drive.

mount /dev/cdrom /mnt

5、Copy file to the /tmp.

cp /mnt/VM*.rpm /tmp

6、Unmount the virtual cd drive.

umount /dev/cdrom

7、Run the installer

rpm -i /tmp/VM*.rpm

8、Run the install script. just hit enter all the way through.

/usr/bin/vmware-config-tools.pl

9、Done.

分类: Linux相关 标签: ,

How to boot Linux in text-only mode

2010年3月19日 12:00 Slyar 2 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

The default runlevel is defined in /etc/inittab

Default runlevel. The runlevels used by RHS are:
0 - halt (Do NOT set initdefault to this)
1 - Single user mode
2 - Multiuser, without NFS (The same as 3, if you do not have networking)
3 - Full multiuser mode
4 - unused
5 - X11
6 - reboot (Do NOT set initdefault to this)

So,If you want to always boot to the text-only mode,edit "/etc/inittab" and change "id:5:initdefault:" to "id:3:initdefault:"

分类: Linux相关 标签:

2010 PI Day (White valentine day)

2010年3月14日 23:26 Slyar 5 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

恩,2010年3月14日,圆周率日,白色情人节。阴天,大雾,温度适宜。

必胜客,情侣南路,湾仔沙。

放一张情侣南路的照片,无聊人士可以对比去年今天的日志...

分类: 大学生活 标签: ,

开始使用Visual Studio 2008

2010年3月11日 11:07 Slyar 5 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

这学期有C++课,老师要求使用Visual Studio 2005/2008,刚好手上有Visual Studio 2008 的英文正式版,只选择了C++组件和MSDN,干掉了将近4G的空间,之后安装了SP1补丁,再挂上Assist插件,整个IDE安装完成。

随便写了几个程序运行调试了一下,发现有了Assist之后VS2008还是非常好用的,尤其是强大的调试功能和代码格式化功能更是让人爱不释手,卸载Cfree和Devcpp,以后就用Visual Studio了。

恩,给新童鞋提个醒,只想看Win32控制台程序运行结果的时候不要按F5(Start Debugging),要按Ctrl+F5(Start Without Debugging),这样控制台界面就不会一闪而过啦。

分类: 大学生活 标签: , ,
bnuep:0801010047