autumn相关日志(2)

将减轻程序编写的麻烦作为人生之目标:-)
Dependency injection is a pattern for designing loosely coupled and testable objects.
 
DI是不是可以当作实现手段之一?
 
C++几个注意的问题
1、使用动态库时,如果是在动态库中申请的空间(如new操作等),必须在这个动态库中释放,不在要其外面释放。即,如果这个库中提供了方法供在库外面调用:
        A* createA() { new A; }
则就需要提供相应的释放方法供在库外面调用:
        void deleteA(A* p) { delete p;}
 
2、使用new char(n)申请的空间,不能使用strcpy()方法来赋值,否则释放的时候会出错。如下代码就会出错:
        char* p = new char(5);
        strcpy(p, "abcd");
        delete p;
 (这儿显然是我写错了,应该是new char[n],而不是new char(n)。    2007-01-04)
 
3、只有使用父类的指针时才能调用到子类的虚函数的实现。所以,如果异常是多层继承的,想在捕捉父类异常时也能通过虚函数获取出在子类实现中所赋的信息,就需要抛出异常的指针,捕捉这个异常指针进行使用,并在使用后释放。根据上述第1个问题,如果异常是在动态库中抛出来的,就不能直接释放。
 
4、临时string变量的c_str()不能用来作为函数的返回值,不要以为它会给返回值维护一个空间,如:
        const char* getStr() { return string("aaaaaa").c_str(); }
临时变量析构后,返回值变空。
 
Autumn平台移植
虽然还没有开发完成,但是还是想在除windows之外的其它平台上试一下。试了两个平台solaris和HPUX,还是vc2005。
一、solaris是自己机器上的虚拟机,是版本10,基本上挺顺利,主要有两点要调整:
(1)原来在windows上,在定义模板的方法时,象以下语句是可以通过的:
    map<K, E>::iterator it = this->HashTable[key % this->Divisor].find(key);
在solaris上,前面必须加上typename。改为:
    typename map<K, E>::iterator it = this->HashTable[key % this->Divisor].find(key);
记得网上有人说必须要加的,但在windows上不加也可以我就没加。所以这儿报错之后,我一下子就想起来了。
(2)继承于exception的异常类必须实现解析方法。我原来没有定义解析方法,编译器提示跟exception的解析方法不一致,于是实现了一个空的方法:
     ~AutumnException() throw() {}

二、HPUX就稍微麻烦一些
(1)没有想到默认的编译选项不认名字空间std,从网上也没有找到相关内容,后来man aCC,才知道应该加选项-AA,加上以后问题就解决了。
(2)编译器是aCC ver A.03.33,标准容器好象实现的不好,我写了一个HashTable的模板,里面使用map<unsinged long, IBeanWrapper*>,但怎么也编译不过去,提示让人感觉莫名其妙,摸不着头脑。猜来猜去,就是这个map使用的问题,只好把后面的指针也换成unsigned long,结果编译通过了。但我写程序就麻烦了,要在指针和unsigned long之间来回转换。幸亏还不是太多。
 
三、VC2005,应该说这个最顺利。因为原来一直是在VC6上调试的。只有一个问题:象atoi、atof、atol之类的都是内联实现,不能直接拿来作为参数了。所以虽然很简单,但还是包了一层,各个都重新写了个方法就过去了。
 
昨晚上定义了一下schema,下一步准备解析XML配置文件了。
 
About string parameter to dynamic library
I implemented a function in dynamic library with string type parameter. When I call it from main preccess, it notes me an _CrtIsValidHeapPointer(pUserData) exceptions. With debugging, I found that the callee has returned. So, I thought string type parameter has a problem. I used "const char*" instead of string, then all is OK.
 
The precision of float and double
For C++, the precision of float is 7 digits and double is 15 digits, although the range of float is 3.4e+/-38 and double is 1.7e+/-308. With using a format of "%.15f", we can display a float in 15 digits. But, it maybe isn't  its true value and have no meaning.
So if we judge that two floats are equal or not, we should use a value of 1e-7 or a larger, not use a value of 1e-10 etc. For example: fabs(f1 - f2) < 1e-7. For double, we should use 1e-15.
 
VC6's BUG!
Last night, I spent a long time to solve a problem about compiling. I defined a template. When I used a type of "string" as parameter, the compiler VC6 noted me that a fatal error happened which is "fatal error C1001: INTERNAL COMPILER ERROR". I searched that error with google. Someone said that it's a bug of VC6 and provided some advice, but it didn't work for me. :(
I had a try with VC2005 express. It's all OK without CppUnit. I found I can't use CppUnit on VC2005. It took me much time to debug them. Finally, I gave up. :(
Now, I use VC6 again, and don't support parameters of string type.
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值