Archive for May, 2010

C++ constructor

May 30th, 2010

C++ derived classed will call parent’s default constructor by default at the beginning of construction. Note, only at the beginning. It’s not like Java, you can call super() at the any time you want.

And if you want to call a parent constructor with parameters (of course, also at the beginning of the construction), you can do it like this:

Derived(int ap, int bp) : Base(ap, bp) {/* do something you want in the derived class constructor */}

The destructor will also call the parent destructor.

Here is a example:

#include <iostream>

using namespace std;

class Base {
int a;
int b;

Base() {
cout << “base constructor” << endl;

Base(int ap, int bp): a(ap) {
b = bp;
cout << “base constructor with param” << endl;

~Base() {
cout << “base destrctor. a: ” << a << “, b: ” << b << endl;

class Derived : public Base {
Derived() {
cout << “derived constructor” << endl;

Derived(int ap, int bp) : Base(ap, bp) {
cout << “Derived constructor with param” << endl;

~Derived() {
cout << “derived destrctor. a: ” << a << “, b: ” << b << endl;

int main() {
Base b(3, 4);
Derived d(3, 4);

compact js

May 28th, 2010

Tool list:

Here are some tools I have found for compact js file in order to save the downloading time.

  • JSMin. It can remove comments and unnecessary whitespaces from js code. The source code (a C file) can be downloaded here. After compilation:

    gcc -o jsmin jsmin.c

    , you can use it like this:

    jsmin <global.js >global.min.js

  • jscompact. It promises to both compact and obfuscate javascript code. However, it depends on ‘Mozilla libjs’ (also called SpiderMonkey) which you can download the src and install it by yourself. NOTE: the newest version of libjs till now is 1.8.0-rc1 which, however is not compatible with the newest jscompact-1.1.1 that (I think) is based on libjs-1.6.0. And if you compile jscompact with js-1.8.0-rc1, you will get errors like this:

    jscompact.c:45: warning: format not a string literal and no format arguments
    jscompact.c: In function ‘shorten_function_vars’:
    jscompact.c:185: error: invalid type argument of ‘->’ (have ‘JSObject’)
    jscompact.c:190: error: ‘js_GetLocalVariable’ undeclared (first use in this function)
    jscompact.c:190: error: (Each undeclared identifier is reported only once
    jscompact.c:190: error: for each function it appears in.)
    jscompact.c:191: error: ‘js_GetArgument’ undeclared (first use in this function)

    OK, here are the right steps to build jscompact. (Remember to replace DIR_LIBJS & DIR_JSCOMPACT with your own dirs.)

    • download libjs-1.6.0 and extract it into DIR_LIBJS.
    • download jscompact and extract it into DIR_JSCOMPACT.
    • compile libjs. Enter DIR_LIBJS/src, compile it with:

      gmake BUILD_OPT=1 -f Makefile.ref

      . And it will build the targets into a new dir Linux_All_OPT.OBJ under src.

    • install the Make a soft link of into /usr/lib

      ln -s -LDIR_LIBJS/src/Linux_All_OPT.OBJ/ /usr/lib/

      which will be used by the final executable jscompact. Of course, you can also simply copy the Linux_All_OPT.OBJ/ into /usr/lib

    • compile jscompact. Enter DIR_JSCOMPACT, and build it:

      gcc -IDIR_LIBJS/src/Linux_All_OPT.OBJ -IDIR_LIBJS/src -g -DXP_UNIX -LDIR_LIBJS/src/Linux_All_OPT.OBJ -ljs -Wall -ojscompact jscompact.c

    Now you can use it like this:

    /jscompact –infile global.js –outfile global.min.js

  • Javascript Minimizer. It’s a .exe file and promises to remove insignificant to javascript characters (comments, tabs, newline, etc…) from *.js files. Haven’t tried yet.


JsMin will removes all the unnecessary white spaces but won’t shorten the function arguments and the local scope variables. JsCompact will remove most of the white spaces and shorten arguments and variables as well. However it leaves a lot of spaces which I think it’s not necessary. Given a 65,280B file, JsCompact reduces the size to 40,532B, while JsMin reduces it to 39,981B.

So I think a good choice is to combine the both. First compact with JsCompact, and remove the spaces with JsMin. With this method, the file 65,280B file can be finally reduced to 37,271B.

Sloving the ajax error c00ce56e

May 28th, 2010

Well, this is an embarrassing error which only occurs in IE but not in Firefox or Chorme. Haven’t tested it in Safari or Opera yet. This error  -”could not complete the operation due to error c00ce56e” – occurs after sending the request and geting the results successfully. It’s due to the encoding problem. I got the solutions here –

  • add the following header on each of the .php files like so:
  • header(“Content-type: text/html; charset=ISO-8859-1″);

  • In your php.ini file, change:
  • default_mimetype = “text/html”


    default_mimetype = “text/html; Charset=utf-8″

    setting virtualbox networking to allow host & guests communicate

    May 21st, 2010

    If you use the default  NAT for as your guest machine’s network adaptor, you can access your host from the guests but can’t access the guests from your host. But in the latest version of VirtualBox, it’s very easy to set the networking so that both host and guests can access each other. You only need to set your network adaptor as “bridge” as shown below:


    setting java environment under ubuntu

    May 20th, 2010

    you can set all the environment vars in /etc/environment.

    for example:



    use bash> source /etc/environment to refresh the system variables.