auto_ptr vs shared_ptr

  • shared_ptr became a standard according to Technical Report 1
  • auto_ptr transfers ownership so that with some caution you can guarantee that only one pointer points to a particular object at any time, whereas shared_ptr provides reference counting so that many pointers can point to the same object
  • you cannot put auto_ptr's in a standard container, but you can with shared_ptr


(via http://www.velocityreviews.com/forums/t283832-boost-shared_ptr-vs-auto_ptr.html)

map vs hash_map

  • STL map accepts almost any type as its element. It does that by relying only on a less-than operation for comparing elements
  • STL map is implemented using red-black balanced tree
  • STL hash_map requires an == and a hash function
  • STL hash_map is implemented by placing a value at its index, unless another value is already placed there, and 'nearby' if one is.
  • map<string, int> m1;
  • hash_map<string, int> m2; // hash using Hash<string>
  • hash_map<string, int, hfct> m3; // hfct() as hash function
  • Prefer a map to hash_map if the elements must be kept in order
  • Prefer hash_map to map when speed of lookup is essential

(via The C++ Programming Language, Third Edition by Bjarne Stroustrup, page 497)

[Advices] Class Hierarchy

  • Use multiple inheritance to separate implementation details from an interface
  • Use a virtual base to represent something common to some, but not all, classes in hierarchy
  • Don't declare data members protected
  • If a class defines operator delete(), it should have a virtual destructor
  • Don't call virtual functions during constructing or destructing

(via The C++ Programming Language, Third Edition by Bjarne Stroustrup, page 425)

[Benchmark] Post increment vs Pre increment

  • I believe that the difference between ++i and i++ is obvious for c++ practitioners
  • Pre-increment operator is faster, because instead of post-increment, it doesn't create a copy of object

// Pre-increment operator for KClass
KClass & operator++() { ++m_position; return *this; }

// Post-increment operator for KClass
KClass operator++(int) {
  KClass copy(*this); 
  ++(*this); // Calls KClass pre-increment operator
   return copy;
}

(via http://en.allexperts.com/q/C-1040/Increment-operators.html)

[Advices] C++ Types and declarations

  • Keep common and local names short, and keep uncommon and nonlocal names longer
  • Maintain a consistent naming style
  • Prefer a plain int over a short int or a long int
  • Prefer a double over a float or long double
  • Prefer a plain char over a signed char or a unsigned char
  • Avoid making unnecessary assumptions about the size of objects

(via The C++ Programming Language, Third Edition by Bjarne Stroustrup, page 85)

[Advices] C++ Fundamentals

  • If two classes have a common interface, make the interface an abstract class
  • If the implementations of two classes have something significant in common, make that commonality a base class
  • If a class is a container of objects, make it a template
  • If a function implements the algorithm for a container, make it a template function implementing the algorithm for a family class
  • If a set of classes, templates, etc are logically related, place them in common namespace

(via The C++ Programming Language, Third Edition by Bjarne Stroustrup, page 16)

How to implement singleton

Singleton is a design patterns, that restricts that object has only one instance.

There are 3 most important things related to singleton:

  • must have public static method that returns the instance of class
  • must have private constructor
  • must have private static variable that points to instance of class

class Logger {
public:
  static Logger* Instance();
private:
  Logger(){};

  static Logger* m_pInstance;

};

Logger* Logger::m_pInstance = NULL;

Logger* Logger::Instance()
{
   if (!m_pInstance)  
     m_pInstance = new Logger;

  return m_pInstance;

}


(via http://www.yolinux.com/TUTORIALS/C++Singleton.html)