Tuesday, November 20, 2012

Why is C++ so popular?

November 20, 2012 3:03 pm

I've never quite understand the attraction of C++ given its (growing) complexity and the many ways to get into trouble. Here is another reason, its verbosity. The following is a fragment of code from an open source library which will remain nameless. The purpose of this code is to read an XML file and validate using a schema.

In C++:

try
{
   xml_schema::properties props; // to store information on the XSD for validation purposes
   props.schema_location ("http://myurl.org/libX/pd/0.1", "../resources/X.xsd");

   auto_ptr myXobject ( libX::pd_0_1::X_ (Xmlfile, 0, props) );
   // [..] do something with myXobject
}
   // validation errors result in an exception being thrown
   catch (const xml_schema::exception& e)
{
   cerr << "X-ML parsing error: " << e << endl;
}

The equivalent in Java is:

MyObj obj = ObjUtil.readFromFile(Xmlfile, "../resources/X.xsd");

and the equivalent in C:

MyObj* obj = readFromFile (Xmlfile, "../resources/X.xsd", &error);
if (!error) {
   // [..] do something with obj
   free (obj);
} else
   printf ("%s", getLastError());

No comments: