Some useful links on c++ constructor

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

http://billharlan.com/pub/papers/Managing_Cpp_Objects.html .

Google code style is a helpfull material for that also.

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Classes

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

http://www.parashift.com/c%2B%2B-faq-lite/ctors.html#faq-10.6 .

Constructor Design, MSDN;

http://msdn.microsoft.com/en-us/library/ms229060.aspx .

Copy constructor,

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Copy_Constructors .

C++ Idioms and Best Practices

http://www.cacr.caltech.edu/~sean/projects/stlib/html/idioms.html .

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

Advertisements

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++
#ifndef CVARIABLELENDATA_HPP
#define CVARIABLELENDATA_HPP

#include <string>

typedef char*	BuffPtr;
typedef size_t	BuffLen;

class CVariableLenData
{
public:
	//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);

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

#endif //CVARIABLELENDATA_HPP

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

using namespace std;

CVariableLenData::CVariableLenData():m_some_int(0),m_some_str(){}

void CVariableLenData::Serialize(BuffPtr &ptr, BuffLen bufflen, BuffLen& writelen) const
{
	if(!ptr)
		return;
	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;
	return;
}

Some links.

Some usefull GCC options for preprocessor

http://sources.redhat.com/gdb/current/onlinedocs/gdb_11.html

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{
public:
virtual void pureVitualMethod() const=0;
};

class ConcreteClass1 :private Interface1{
public:
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 {
private:
unsigned char* _mhashValue;
public:
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{
public:
using HashEnvelop::getHashValue;
void calculateHashValue(const unsigned char * messageBytes){
// Calculate the hash value using MD5 algorithm
// and stores the value (in messageBytes)
}
};

Reference Readings :

  1. http://www.cprogramming.com/tutorial/private.html
  2. Design Patterns Elements of Reusable Object-Oriented Software, by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
  3. http://www.parashift.com/c++-faq-lite/private-inheritance.html