Some useful links on c++ constructor

There are lot of issues around c++ constructors, and I have few links that may help; .

Google code style is a helpfull material for that also.

constructors should initialize as a rule all member objects in the initialization list .

Constructor Design, MSDN; .

Copy constructor, .

C++ Idioms and Best Practices .

One very important point (my learning), you should have default constructor defined, either private or public !

simple C/C++ Serialization or deflating or Marshalling user defined data types or objects

But is it to use boost or not it is your decision 🙂  It is a good lib. But I find it very handy for small in another way as below;-

// Just a simple serialization in C++

#include <string>

typedef char*	BuffPtr;
typedef size_t	BuffLen;

class CVariableLenData
	//Serilizes data members to a char buffer
	void Serialize(BuffPtr &ptr, BuffLen len, BuffLen &writelen) const;

	//DeSerilizes from a char buffer & constructs an object
	static CVariableLenData* DeSerialize(const BuffPtr ptr, BuffLen len);

	void SetData(std::string& str);
	void SetData(int& val);

	int m_some_int;			// fixed length
	std::string m_some_str; // variable length
	CVariableLenData(const CVariableLenData&);


// Source file
#include <cstdio>
#include "CVariableLenData.h"

using namespace std;


void CVariableLenData::Serialize(BuffPtr &ptr, BuffLen bufflen, BuffLen& writelen) const
	writelen = _snprintf (ptr, bufflen,"%d", m_some_int);
	*(ptr+writelen) = ' ';		// after each numeric value add an space
	size_t len = writelen + 1;
	// now write the string
	// copy length
	writelen = _snprintf ( (ptr+len), bufflen,"%u", m_some_str.length());
	*(ptr + len ) = ' ';
	len = len + writelen + 1;
	// Copy the data
	memcpy( (ptr+len), m_some_str.c_str(), m_some_str.length());
	writelen = len;

Some links.

Some usefull GCC options for preprocessor

We pass the `-gdwarf-2′ and `-g3′

flags to ensure the compiler includes information about preprocessor macro. For example;-

gcc -gdwarf-2 -g3 sample.c -o sample

To print the preprocessor output and stop there :
gcc -E sample.c

Private Inheritance in C++

“Why Private Inheritance in C++ at all?” Concrete Classes in C++ are pretty easy to understand but what is not easy is Interface (Java style Interface), though C++ claims similar functionality using the Pure Abstract Class. Pure Abstract Class consists only Pure Virtual functions not any data (except the static one). In this way Interface (or Pure Abstract Class) refers to type of an object (instance of a class) not the Class of the object.

class Interface1{
virtual void pureVitualMethod() const=0;

class ConcreteClass1 :private Interface1{
void pureVitualMethod() const;

Now inheriting privately the Interface (as done in above code snippet) makes no use at this point of time as they will be inaccessible through the Interface’s pointer.

But class inheritance will aid you to define new objects with respect to the old class(Base class). How about inheriting it privately?

  • You cannot access derived class’s object through base class’s pointer.
  • You cannot access public or protected member function of the base class though the derived class’s object

Then the next question would be what is the use? The reasons are as follows;-

  • Pure Class inheritance (not subtyping) can be achieved via private inheritance in C++.
  • private inheritance will result in Composition (or, it exhibits the “has a” relationship rather than “is a” relationship)
  • can be necessary when you need to override a virtual function

Let see the following example, which utilizes a private inheritance,-

class HashEnvelop {
unsigned char* _mhashValue;
unsigned char* getHashValue(){
return _mhashValue;

/* Some other functions related to Hash calculations

virtual void calculateHashValue(const unsigned char* messageBytes){
// Calclates hash value for the
// specified input bytes (messageBytes)
// and stores the claculated value(in _mhashValue)

class MD5HashEnvelop: private HashEnvelop{
using HashEnvelop::getHashValue;
void calculateHashValue(const unsigned char * messageBytes){
// Calculate the hash value using MD5 algorithm
// and stores the value (in messageBytes)

Reference Readings :

  2. Design Patterns Elements of Reusable Object-Oriented Software, by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides