Some Interesting Things In C++
I found out some interesting things when making this persistent file based order tracking system which I was asked to code before an interview.
- In a copy constructor, if your Object(const Object & rhs) is not declared as const, you get an error saying: "no copy constructor available or copy constructor is explicit". This happens on VS.Net professional 2003 and might happen on other compilers. Interesting, I never thought the a compiler will complain (specially with a misleading error message) for not declaring something as const.
- To make integral type member data as consts, you must declare them as static const.
- Related to 2. To make static integral member data (which are not const), you must define it outside the class in C++ and use the scope resolution operator on it.
- Related to 2 and 3 . To declare Non integral member data (such as arrays or strings) as consts you must do 2. AND you must define them outside of the class like 3.
- Summary: Related to 2, 3, and 4. Only static const integral types can be defined inside the class. Any consts members must be static const. Any static members (except for static const integral members) must be defined outside the class with the scope resolution operator.
- by making a const function, you are telling the compiler, that I am not gonna change the (this) object inside this function.

1 Comments:
answer to number 6:
reference syntax is easier to read than pointer syntax.
Post a Comment
<< Home