Thursday 22 December 2005

Delphi constructors with different names, when used in C++Builder

The integration of C++Builder and Delphi seems to have caused a few problems for those of us who write components or classes in Delphi, and then want to use these in C++Builder. There are a few things one should avoid when one writes classes (see below), and the symbol BCB doesn't seem to be defined when the Delphi compiler is called from C++Builder.

Some of the things one should not do are mentioned on the JVCL sourceforge pages.

One other thing is something I discussed elsewhere with someone. In Delphi, it is not unusual to create multiple constructors with different names which tell the user what they do.

A simple example I concocted up is below:

unit GraphicVectors;

interface

uses
  SysUtils, Classes;

type
  TGraphicVector = class(TObject)
  public
    constructor Create(X, Y: Double);
  end;

  TPolarVector = class(TGraphicVector)
  public
    constructor CreatePolar(Radial, Theta: Double);
  end;

implementation

This defines TGraphicVector, which takes two Cartesian coordinates, X and Y, both of type Double. And a descendant TPolarVector, which is internally almost the same, but can be defined using polar coordinates. So TPolarVector has two constructors, the one inherited from TGraphicVector, and CreatePolar, which accepts polar coordinates.

Constructors which differ only in name

Problem is, that both have two parameters of type Double. In Delphi, this is not a problem, because the constructors have different names. But C++ constructors do not have any names of their own. They get the name of the class for which they are defined.

So the .hpp file for the unit above contains this, for TPolarVector:

class DELPHICLASS TPolarVector;
class PASCALIMPLEMENTATION TPolarVector : public TGraphicVector
{
    typedef TGraphicVector inherited;

public:
    __fastcall TPolarVector(double Radial, double Theta);
public:
    #pragma option push -w-inl
    /* TGraphicVector.Create */ inline __fastcall
        TPolarVector(double X, double Y) : TGraphicVector(X, Y) { }
    #pragma option pop

public:
    #pragma option push -w-inl
    /* TObject.Destroy */ inline __fastcall virtual ~TPolarVector(void) { }
    #pragma option pop

};

You can see the problem above: there are two constructors TPolarvector(double, double). The first is TPolarvector.CreatePolar, the second is TGraphicVector.Create, which must be redeclared as TPolarVector to make it a constructor for TPolarVector as well. C++ does not allow two constructors with the same signature, so you will get two errors for the second declaration:

[C++ Error] GraphicVectors.hpp(49): E2238 Multiple declaration for '_fastcall TPolarVector::TPolarVector(double,double)'

[C++ Error] GraphicVectors.hpp(46): E2344 Earlier declaration of '_fastcall TPolarVector::TPolarVector(double,double)'

Conclusion

So, apart from what the JVCL pages describe, you should also avoid using constructors with the same signature but differing in name. It is best if you simply do not use any other constructor name than Create. If you need more than one, be sure to use the overload directive.

Rudy Velthuis

Sunday 12 June 2005

Quoting in newsgroups

Newsgroups are a great way to communicate. One of the big advantages is, that you don't have to reply immediately, and that everyone can see your reply. It also means that more than one person can jump into the conversation going on, and write a reply, whenever that person thinks it is convenient to do so. This means that a thread in a newsgroups is not linear or chronological, but can have many sub-branches.

If you scan a newsgroup for a new, unread reply, the reply you find may be a reply to a message made a few days ago, and in a completely different thread or subthread than the message you were reading only a few minutes ago. To remind you of the context of the reply, i.e. to give the reader an indication to what the poster was replying, it is usual to quote (cite) a part of the message to which the reply was made. To make it apparent what was quoted, the quotes are usually marked with a special character, often >, at the start of every line. Many newsreaders allow you to automatically quote the entire previous message when you write a reply. That is of course very convenient for the poster, but it can be a problem for the reader.

If everyone simply quoted the entire previous message (with previous I mean the message to which the reply is made), the final message contains quote over quote of all the previous messages, so more or less the entire thread leading to that point. This may be fine for those who start reading in the middle of a thread, but it wastes an enormous amount of bandwidth, and it gets harder and harder to actually find the actual reply, especially since some people reply at the start of the message (i.e. above the quotes) and some reply below the quotes, or inline, i.e. replying separately to each point made.

In Usenet, a large set of mostly freely available newsgroups shared across many servers around the world, people have tried to define standards for quoting, to avoid this terrible mess. A short version of these (very inofficial) rules, with my personal comments, is:

  • Mark your quotes with a > character. There are different ways of marking a quote, but the > has become some kind of de facto standard, and any other way of quoting makes your replies harder to read, for many people.

  • Reply below the quotes, or inline (which is similar). I know this is a controversial point, but IMO this is the only logical way of replying. Like someone said:

    A: Because it fouls the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Top-posting is putting your reply above the quotes. One other disadvantage is that someone who expects replies below the quotes, or inline, will have to scroll to the end of the message to see if there is more.

  • Do not overquote. Delete those parts of the quotes which are not necessary to assist the reader in remembering what the previous message was about. If the relation between quotes and new content of a message is grossly in favour of the quotes, one may talk about overquoting. This is a nuisance, since it requires people to read or scroll over a lot of stuff before they see any reply. It also wastes bandwidth, and space on the harddisks of your readers.

  • Do not underquote. If you don't quote at all, or not enough, the reader does not have a context, and may be forced to re-read the previous message to remember what is going on, or to see to what you are actually replying.

These are my personal views on quoting, and others may have different opinions. But I hope to have given a hint on how newsgroup reading can be made more pleasant. You don't have to follow my rules, but notice that if you don't, you might be doing your readers a disservice, and they might decide that they'd rather not read your messages anymore.

Update: I came across this article from a blind person about top-posting. Top-posting seems to confuse a popular newsreader for the blind. One more reason to abandon it, IMO.

Rudy Velthuis

Links