Using Delphi Sets in C++ Builder

I find myself having to do a project in C++ Builder. I have been using Delphi since Delphi 2 but have never had to do a VCL project using C++ Builder before. Mostly, it isn’t a problem. Just mentally convert from Delphi syntax to C++ syntax and away you go. Unfortunately, sometimes you run across something that is an integrated part of the Delphi language that has to be emulated in some other way in C++. Sets are one of those things.In C++ Builder, sets are emulated using a template class. To declare a set based on an enumerated type you declare a class like this (using TFont.Style as an example):

enum TFontStyle { fsBold, fsItalic, fsUnderline, fsStrikeOut };
typedef Set<TFontStyle, fsBold, fsStrikeOut> TFontStyles;

Since sets are emulated by a class, you no longer have built in language features that you can use to manipulate them. Everything is now done with operators and methods.

To assign a particular value to a set; in Delphi:

Font.Style := [ fsBold, fsItalic ];

in C++:

Font->Style = TFontStyles();
Font->Style = Font->Style << fsBold << fsItalic;

In C++ you can also remove items from the set using the >> operator:

Font->Style = Font->Style >> fsBold;

To empty a set; in Delphi:

Font.Style := [];

in C++:

Font->Style = TFontStyles();

The +, - and * operators work the same way in both Delphi and C++.

In the course of researching the little problem I found an excellent introduction to the differences between Delphi and C++ Builder here.

2 Responses to “Using Delphi Sets in C++ Builder”

  1. Moritz Beutel Says:

    Not quite correct.

    > Font->Style.Clear();
    > Font->Style = Font->Style Style.Clear() doesn’t do anything if Font->Style has a setter function. The right way would be:

    Font->Style = TFontStyles ()

  2. admin Says:

    Thank you for pointing this out to me. I have updated the article.

Leave a Reply