DuraSoft  TechLet 01, 2003

In this issue
Welcome to the January 2003 TechLet. If you have received this through some one you know and would like to subscribe directly for our future issues, please send an email to techlet@durasoftcorp.com with a subject subscribe. This subscription currently is absolutely free.

In this issue, we have a detailed article on XML Serialization in .NET, and we will see how easy it is to mix and transform between C# (or any .NET language) objects and XML.

Message from us
If you like any question to be addressed or topic to be discussed, please send us an email to techlet@durasoftcorp.com. At the end of the year, one person will be randomly selected from the list of respondents for a special prize. We would like to hear from you.

Winner from 2002!: One person was chosen randomly on December 23rd from the Techlet subscribers who wrote to us in 2002. Congratulations to Vijayabaskar Gangadar who is the winner of a $100 gift certificate from Amazon.com.

Please send suggestions, corrections and comments to techlet@durasoftcorp.com. Wish you all a very happy, safe and prosperous new year!

In depth
XML Serialization in .NET? XML Serialization in .NET provides ease of development, convenience and efficiency. This article discusses the benefits of XML serialization and ways to configure the generated XML document. It also goes further to discuss some of the issues and deficiencies of this process as well. The complete article can be found at http://www.durasoftcorp.com/download
Quiz corner
In the article "How to create/avoid memory leak in Java and .NET?" we discussed how .NET provides the IDisposable interface as a streamlined standard interface for resource garbage collection. A user of an object should check to see if the class implements the IDisposable interface and then call the Dispose method to release the resources utilized by the object. What keyword does C# provide to help with this process, in a way that this dispose mechanism may be semi automated.


Quiz from the past issue

This month's quiz is exclusively for those who consider themselves savvy in C++. In the June article, we discussed about the issues with implementing the equals method in Java. The solution pretty much addresses how you can solve the problem in a language (like Java, .NET languages) that supports reflection. It got me thinking about how one can solve the problem of equals (operator==) in C++ efficiently. So, here is the question to you.

Can you implement the operator== for the code given below such that it produces the correct result?

#include 
using namespace std;

class Point
{
	public:
		// Showing only minimal code.
		virtual ~Point() {}
		Point(int px, int py) : x(px), y(py) {}
		virtual bool operator==(const Point& other) const
		{
			// Your code?
		}
	private:
		const int x;
		const int y;
};

class ColorPoint : public Point
{
	public:
		ColorPoint(int px, int py, int clr) : Point(px, py)
		{
			color = clr;
		}
	
		virtual bool operator==(const Point& other) const
		{
			// Your code?
		}
	private:
		int color;
};

void main()
{
	ColorPoint p1(1, 2, 5);
	Point p2(1, 2);
	ColorPoint p3(1, 2, 6);
	ColorPoint p4(1, 2, 5);

	cout << (p1 == p2) << endl;
	cout << (p2 == p1) << endl;
	cout << (p1 == p2) << endl;
	cout << (p2 == p3) << endl;
	cout << (p3 == p1) << endl;
	cout << (p1 == p4) << endl;
}				

Answer!

There are two answers to this question.

Answer 1:
Point::operator== method:

virtual bool operator==(const Point& other) const
{
	int* ptrToMyVptr = (int*) this;
	int* ptrToOthersVptr = (int*) &other;

	void* pMyVPtr = (void*) *ptrToMyVptr;
	void* pOthersVptr = (void*) *ptrToOthersVptr;

	if (pMyVPtr == pOthersVptr)
	{
		if (x ==  other.x && y == other.y)
			return true;
	}
	return false;
}
ColorPoint::operator== method:
virtual bool operator==(const Point& other) const
{
	int* ptrToMyVptr = (int*) this;
	int* ptrToOthersVptr = (int*) &other;

	void* pMyVPtr = (void*) *ptrToMyVptr;
	void* pOthersVptr = (void*) *ptrToOthersVptr;

	if (pMyVPtr == pOthersVptr)
	{
		const ColorPoint* pOtherColorPoint = 
				dynamic_cast(&other);
		if (pOtherColorPoint == 0) return false;

		if (Point::operator==(other) && pOtherColorPoint->color == color)
			return true;
	}
	return false;
}
The above code gets the pointer to the virtual table from the two objects. If the classes of the two object's are the same, then the virtual table pointers must be equal. Of course, one could get creative and make the above code more cryptic and shorter.
But a more sophisticated answer, if you will, would be the following:
Answer 2:
It is to use some thing close to reflection in C++. First #include<typeinfo.h>. Then you can use the typeid operator to get the type information of the object at run time.
Point::operator== method:
virtual bool operator==(const Point& other) const
{
	if (typeid(*this) == typeid(other))
	{
		if (x ==  other.x && y == other.y)
			return true;
	}
	return false;
}
ColorPoint::operator== method:
virtual bool operator==(const Point& other) const
{
	if (typeid(*this) == typeid(other))
	{
		const ColorPoint* pOtherColorPoint = dynamic_cast(&other);
		if (pOtherColorPoint == 0) return false;
		if (Point::operator==(other) && pOtherColorPoint->color == color)
			return true;
	}
	return false;
}

Thus spake...
Everything should be made as simple as possible, but not simpler - Albert Einstein.