Tips on using std::vector
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/
Traits
Concepts
http://www.cantrip.org/traits.html
Some uses
http://www.gotw.ca/gotw/071.htm#5
Hierarchy Wide Traits
http://erdani.org/publications/traits_on_steroids.html
Is Pointer Trait
template <typename T> class IsPointer
{
public:
enum
{
Answer = false
};
};
template <typename T> class IsPointer<T*>
{
public:
enum
{
Answer = true
};
};
Usage
if(IsPointer<T>::Answer)
{
// Yes
}
Is Class D Derived From Class B
template <class D, class B> class IsDerivedFrom
{
private:
static bool Test(void*)
{
return false;
}
static bool Test(B*)
{
return true;
}
public:
static bool Answer()
{
D* ptrD = 0;
return Test(ptrD);
}
};
// Specialization of IsDerivedFrom
template <class D, class B> class IsDerivedFrom<D*,B*>
{
private:
static bool Test(void*)
{
return false;
}
static bool Test(B*)
{
return true;
}
public:
static bool Answer()
{
D* ptrD = 0;
return Test(ptrD);
}
};
Usage
if(IsDerivedFrom<Derived,Base>::Answer())
{
// YES
}
note the template specialization is there so that if the user passes in pointers to classes as the paramaters it returns the same result as if they were classes i.e.
if(IsDerivedFrom<Derived*,Bass*>::Answer())
{
// YES
}
Function Prototypes for serialization
class Object
{
friend std::ostream& operator<< (std::ostream& o, const Object& obj);
friend std::istream& operator>> (std::istream& i, Object& obj);
friend std::ostream& operator<< (std::ostream& o, const Object*ptrObj);
friend std::istream& operator>> (std::istream& i, Object*&ptrObj);
}
Note, if you derive from Object then will automagically call the methods in the base class above on that object except for the last one which will need to be implemented for each class type. A good way to do this is to #define it as boiler plate code.
Using Files and ofstream, ifstream
http://www.cplusplus.com/doc/tutorial/files.html