c++ is a superset of c which has extra OOP and generic programming capability.
K&R C is classic C. new standard for C is ANSI/ISO C, a.k.a C99
#include
always use long variable for data longer than 32bit for the sake of good portability.
const vs #define : 1 explicitly declared type 2 used in scopes
losing precision of floating number. e.g. 2.34E+22 + 1
function pointer : int* func() –> int* (*func)()
two-dimension array processing function : void func(int arr[][4]) or void func(int (*arr)[4] )
implicit type conversion: using one argument constructor / use explicit keyword to turn it off
copy-constructor: assignment operator should perform deep copy.
copy-constructor should check whether internal static member is changed in ordinary constructor
assignment-operator should avoid copying from self to self
copy-constructor is invoked when 1. passing argument as value 2. return object as value
3
when doing member function overloading, const keyword is also taken into account.
e.g.
const char & operator[](int i) const {return str[i];}
char & operator[](int i) {return str[i];}
the former is for ordinary object, the later is for const object.
assignment and type conversion:
A a; B b; b= a;
1. a is converted to a temporary B object using conversion constructor: B(const A a)
2. the previous temporary B object is assigned to b using assignment operator
more efficient way:
overload the assignment operator letting it able to handle the assignment between A and B objects directly.
e.g.
B & operator=(A const & a)
deep-copying :
1 constructor : use constructor list
2 destructor: apply memberwise deleting automatically.
3 copy-destructor: apply memberwise copying.
4 assignment-operator : explicitly use base class assignment operator as BaseClass::operator=(obj) to copy the portion of base class. Then do memberwise copying.
STL auto_ptr is a possessive smart pointer, which means it will eliminate the pointer copied to it.
need more discussing http://en.wikipedia.org/wiki/Smart_pointer
problem sovled : 1 memory leaking 2 object slicing
difference between memcpy and memmove
whether can handle memory overlapping
difference between private inheritance and containment
1 private inheritance can redefine virtual functions, containment not
2 private inheritance can access protected members, containment not
3 containment can use several objects, private inheritance uses only one
stl container category :
sequence container
1 vector
2 lists
3 deque (double ended queue)
associative containers
1 map
2 multimap
3 set
4 multiset
stl iterator category
input iterator
output iterator
forward iterator
bidirectional iterator
random_access_iteraotor
forward declaration
when using friend class like this: B::foo() is friend class to A
Class B{
void foo(A& a);
}
Class A{
friend void B::foo(A& a);
}
in A’s declaration, the code refers to class B, therefore, B’s declaration must appear before.
However, in B::foo(), it also references class A. Then, forward declaration of A should be used here.
Notice, in B’s declaration, only function prototypes are allowed, otherwise, if a method definition reference A,
the compiler has to inspect the whole content of A, which is unavailable right now.
By the way, making the entire class B a friend doesnt need a forward declaration, because the statement
friend class B;
already indicates that B is a class.
return type is not involved in generating a member function’s signature. that is to say, you can override a function with a different return type, as long as the new return is derived from the previous one.
the default behavior of meeting a uncaught exception is to call terminate() then abort(); set_unexpected(handler) can override this behavior.
One Response
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
No credit car need here for fresh updates