Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VFP versus C++
Message
General information
Forum:
Visual FoxPro
Category:
Other
Title:
Miscellaneous
Thread ID:
00842594
Message ID:
00843548
Views:
25
Leland,

At first impressions it is easy to generalise C++ as being a "bolt on" or extension to C but I don't believe this to be the case. I think the reason for this generalistation (and also the percieved complexity of C++) is its strong compatibility with C. C++ core features like advanced templates, exception handling and even the bool datatype are all evolutionary changes to the core language and have come about largely due to standardisation.

As for C vs. C++ speed the following example illustrates the concept.

In a lot of C tutorials/examples you will see dispatch code based on a switch (selection) statement similiar to this, this code will not compile it is for illustrative purposes only.
void ExpandPolygon(Polygon *poly)
{
   switch (poly->type)
      {
      case RECTANGLE:
         ExpandRectangle();
         break;

      case CIRCLE:
         ExpandCircle();
         break;

      //
      // So on and so forth...
      //

      }
}
There are two main issues with this code. Firstly you have the overhead of a function call just to get to the point of determining type and how to proceed. Secondly the switch statement boils down to a series of if..else's which is not fast. Given these issues then the following C++ code is typically three-four times faster.
struct Polygon
{
   int x;
   int y;
   
   virtual void Expand()
   {
      x += 10;
      y += 10;
   }
};

struct Rectangle : public Polygon
{
   void Expand()
   {
      x += 10;
      y += 10;
   }
};

struct Circle : public Polygon
{
   void Expand()
   {
      x += 20;
      y += 20;
   }
};
Now this code is faster because the C++ compiler knows the type and resolving the call by building us a vtable (or jump table).

Regards
Neil
Previous
Reply
Map
View

Click here to load this message in the networking platform