C Plusplus
  • The Design and Evolution of C++
  • The Annotated C++ Reference Manual
  • The C++ Programming Language 3rd
  • C++ Primer Plus 5th by Stephen Prata
  • Accelerated C++
  • The C++ Standard Library: A Tutorial and Reference
  • The C++ Standard Library Extensions
  • C++ Templates: The Complete Guide
  • C++ Common Knowledge
  • Thinking in C++
  • C++ How to Program
  • The ANSI C++ standard says that const and & should not be used to determine a function's signature.
double AddOne(double x);
double AddOne(double& x);
double AddOne(const double x);
double AddOne(const double& x);
  • C++ primer
    • Overloaded function
    • Overloaded operators and User-defined conversions
  • The default access level for class members is private.
  • Private is the default inheritance level.
  • Public inheritance
Base access specifier Derived access specifier Derived class access? Public access?
Public Public Yes Yes
Private Private No No
Protected Protected Yes No
  • Private inheritance
Base access specifier Derived access specifier Derived class access? Public access?
Public Private Yes No
Private Private No No
Protected Private Yes No
  • Protected inheritance
Base access specifier Derived access specifier Derived class access? Public access?
Public Protected Yes No
Private Private No No
Protected Protected Yes No
class Qux
                Qux() : _foo( "initialize foo to this!" ) { }
                // This is nearly equivalent to 
                // Qux() { _foo = "initialize foo to this!"; }
                // but without the extra call to construct an empty string
                std::string _foo;
  • During base class construction, virtual functions never go down into derived classes.
  • dynamic_cast Operator
    • The value of a failed cast to pointer type is the null pointer.
    • A failed cast to reference type throws a exception.
    • You cannot use dynamic_cast to convert from a non-polymorphic class (a class with no virtual functions). You can use static_cast to perform conversions of non-polymorphic types.
  • upcast
class B { ... };
class C : public B { ... };
class D : public C { ... };

void f(D* pd)
   C* pc = dynamic_cast<C*>(pd);   // ok: C is a direct base class
                                 // pc points to C subobject of pd 

   B* pb = dynamic_cast<B*>(pd);   // ok: B is an indirect base class
                                 // pb points to B subobject of pd 
  • downcast
class B { ... };
class D : public B { ... };

void f()
   B* pb = new D;                     // unclear but ok
   B* pb2 = new B;

   D* pd = dynamic_cast<D*>(pb);      // ok: pb actually points to a D
   D* pd2 = dynamic_cast<D*>(pb2);   // pb2 points to a B not a D
                                    // cast was bad so pd2 == NULL
  • cross cast
void f(D* pd)
   B* pb = dynamic_cast<B*>(pd);      // cross cast
  • Copy constructor makes a deep copy like assignment, but it is somewhat simpler:
    • There is no need to test to see if it is being initialized from itself.
    • There is no need to clean up (eg, delete) an existing value
      • If it is assigned, there is an existing value before the assignment.
    • A reference to itself is not returned.
      • There is no return in any constructor.
CFoo::CFoo(const CFoo& obj) // copy constructor

CFoo& CFoo::operator=(const CFoo& rhs) 
p = q;              // Assignment operator, no constructor or copy constructor.
A::operator=(const A&)
A::operator=(volatile A&)
A::operator=(const volatile A&)
  • If you do not declare a copy assignment operator for a class A, the compiler will implicitly declare one for you which will be inline public.
  • Introduction to Android C++ reference counting part 1, part 2
    • The usage of weak pointer
      • Hold a weak reference to observers. Only notify observer when it is alive.
      • To solve the memory leak issue involved in the circular strong reference. Suppose A hold a strong reference to B and B hold strong reference to A. Then, neither A or B will be destroyed since there is always one strong reference to them. One of the solution is to make one of reference be weak reference.
  • boost weak_ptr class template
    • A weak_ptr can only be created from a shared_ptr.
    • The weak_ptr class template stores a "weak reference" to an object that's already managed by a shared_ptr.
    • To access the object, a weak_ptr can be converted to a shared_ptr using the shared_ptr constructor or the member function lock. When the last shared_ptr to the object goes away and the object is deleted, the attempt to obtain a shared_ptr from the weak_ptr instances that refer to the deleted object will fail.
    • Access its stored pointer is often dangerous in multithreaded programs. The solution to this problem is to create a temporary shared_ptr from it.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License