1. Operator overloading must have user’s self-definition argument.
2. void * can be evaluated by any point-type data except point to function.
3. void f(int i) and void f(const int i) can’t be declared at the same time. They are not overloaded functions. It products a compiled error. But if we add “const” or “volatile” to point or reference type argument, the compiler will look on these functions as two different functions.
4. Static data type can be declared as its affiliated class’s type in class body. But non-static data type can be only declared as its affiliated class’s point or reference in class body. See the following example:
class Bar {
public:
static Bar mem1; // ok
Bar* mem2; //ok
Bar mem3; //error
};
5. Arbitrary function point can be converted to void(*)() type by reinterpret_cast<>. And arbitrary functor point can be converted to void* type by static_cast<>
6. Any functions that don’t modify member field should be declared as const function. But if you wanna modify member field in const function. How to do it? Here lists two methods:
a. In class Y has a method like “void Y::f() const”. We can do some modifies in function body: void Y::f() const { ((Y*)this)->j++; } we convert this point to Y* compulsorily.
b. Using “mutable” keyword. mutable int j;
7. If a class type wants to be put into an union, it should meet demands as follow:
a. The class type‘s construction, destruction, assignment and copying functions should be generated only by compiler.
b. Non virtual functions and virtual base classes.
c. All the base classes and no-static member should obey above-mentioned two rules.
8. Non template function can exist with function template with same name. When overloading takes affect, Non template function will be selected preferentially. In ordinary functions argument can be converted automatically. But in function template it is impossible.
9. Nontype template parameters have some restrictions. Usually they should be const int type(including enum ,static class member and extern point or reference type).
template<char const* name>
class Myclass { };
extern char const s[] = “hello”; // without this declaration, an error will output
Myclass<s> x;
10. A function produced by function template will never looked on as same as ordinary function, even if they have same type and name. This rule will cause the following result:
a. Those functions generated by member function template will not override virtual functions.
b. Those functions generated by constructor template will not regard as default copy function.
11. To judge if a type can be converted to another type We have the following method:
template<class T,class U>
class Conversion {
typedef char Small; // the length is 1
class Big { char dummy[2]; }; // the length is 2
static Small Test(U);
static Big Test(…);
static T MakeT();
public:
enum { exists = sizeof(Test(MakeT()) == sizeof(Small)};
};
Use method: cout << Conversion<double,int>::exists;
With help of “Conversion” it’s easy to judge extend-relationship between two classes:
//partial specialization
template<class T>
class Conversion<T,T> {
public:
enum { exists =1, exists2Way =1,sameType = 1 };
};
#define SUPERSUBCLASS(T,U) \
(Conversion<const U*,const T*>::exists
&& !Conversion<const T*,const void*>::sameType)
Specification from <<modern c++ design>> 2001
12. c++ exception process system require all the objects in stack those destructor be declared as public.