Tuesday, November 21, 2006

C++ Programming Insights

1. Difference between Static and Const

Basically, static is a prefix for the storage. When you declare it before a variable, function, a class, you actually mean it is stored at the beginning of a program and will destroy at the end of the program. A static member data can only be called by a static member function in a class (??)

So it will natural that a static member of a class does not need an specific object existing, you can access it by :: without referring to any real objects.

However, const is a concept about the changing. A const variable can't be changed during funcation call, a const object must be initialized at the begining, and a const data can't be modified during the program.

2. const function declaration:

const int get(); a function will return a const int
int get() const; a class member funtion that is a const, will not change any values.
int get(const); a function whose member arguments are const, which will not be changed during this function call.

3. Copy constructor.

A copy constructor must use a reference to another object to avoid executing the default constructor infinite time.

No comments: