2 Tips of C++ Programming with const

1. Implicit type conversion and copy constructor

If you want to define your own copy constructor, you should define like that:

class Foo {
public:
  Foo (const Foo& obj) {}
};

Pay attention to the const in the parameter declaration. If your copy constructor definition loses const (Sun's C++ compiler does NOT complain), your implicit type conversion member will fail to compile, though the copy constructor is not used in type conversion.

class Foo {
public:
  Foo (Foo& obj) {}
  Foo (Bar *obj) {}
};

Foo test () { return new Bar(); }

On linux with g++, you will get the error messages as following:
  error: no matching function for call to ‘Foo::Foo(Foo)’
  ... ...
  error:   initializing temporary from result of ‘Foo::Foo(Bar*)’

On solaris with sun studio c++ compiler, you will get:
  Error: Cannot use Bar* to initialize Foo without "Foo::Foo(const Foo&)"

2. The reference of pointer and "this"

Look at this piece of code:

void test (Foo* &obj) {}
//void test (Foo* const &obj) {}

class Foo {
public:
  void bar () { test (this); }
};

On linux with g++, you will get error messages as following:
  invalid initialization of non-const reference of type ‘Foo*&’ from a temporary of type ‘Foo* const’
  in passing argument 1 of ‘void test(Foo*&)’

On solaris with sun studio c++ compiler, you will get:
  Error: Formal argument obj of type Foo*& in call to test(Foo*&) requires an lvalue.

Seems that sun studio c++ compiler treats the & in "&obj" as the addressing operator, so it requires an lvalue (left value). If we uncomment the 2nd test () (which in blue), then in function bar (), we must call test ((Foo *const) this); or just comment out the 1st test (), so that we could pass the compiling.

For non const members or external functions, the type of 'this' is Foo *const, for const members, its type is const Foo *const.

李娜进入温网8强!

李娜历史性的进入了温网的八强!

看了北京电视台的录播,李娜的底线正手进攻非常犀利,速度和角度完全压制了捷克选手瓦伊迪索娃的进攻。对手只有在发球中尝试突破,但在一发出现失误时,二发多次被李娜直接反击得分。

期待李娜在下一场对小克的比赛中再创佳绩。