Visibility of class members

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Class > Visibility of class members

 

In Delphi Members at the beginning of a VCL class declaration that donít have a specified visibility are by default published and in other classes they are public. In C++ this is written explicitly.  (DelphiXE2Cpp11 ignores the {$M+} directive, which would make them public.)

 

A problem at the translation of Delphi code is, that in Delphi a private or protected member is visible anywhere in the module where its class is declared. In C++ a private or protected member is visible only inside of the class. DelphiXE2C++11 solves this problem by making all classes in the same module to friends of each other. Free routines also are declared as friend.

 

The following Delphi code is an example, where the direct translation to C++ code would not compile, if  TFriend isn't declared as a friend of TLonely:

 

TFriend = class

private

  FCount: Integer;

end;

  

TLonely = class(TFriend)

public

  procedure NeedsFriend;

end;

 

implementation

 

procedure TLonely.NeedsFriend;

begin

  FCount := 0; // in C++ access to TLonely::FCount is not possible

end;   

 

 

The converted C++ code doesn't compile, because FCount cannot be accessed in TLonely::NeedsFriend.

 

 

unit friends;

 

type

 

class TFriend : public TObject

{

private:

       int FCount;

public:

       __fastcall TFriend();

};

 

class TLonely : public TFriend

{

public: 

       void __fastcall NeedsFriend();

       __fastcall TLonely();

};

 

 

 

void __fastcall TLonely::NeedsFriend()

{

bool result = false;

FCount = 0; // in C++ access to TLonely::FCount is not possible

return result;

}

 

 

DelphiXE2Cpp11 therefore lists all possible class- and routine- friend declarations of a unit into a file and includes it into all class declarations. The name of this file is created by appending "_friends" to the file name. The file gets the extension ".inc". The class declarations of the example therefore becomes to:

 

 

class TFriend : public TObject

{

#include "friends_friends.inc"

private:

       int FCount;

public:

       __fastcall TFriend();

};

 

class TLonely : public TFriend

{

#include "friends_friends.inc"

public: 

       bool __fastcall NeedsFriend();

       __fastcall TLonely();

};

 

 

The content of "friends_friends.inc" is:

 

friend class TFriend;

friend class TLonely;

 

Now TLonely::NeedsFriend compiles without problem. If this function were no member function, but a free function like:

 

 

 

procedure NeedsFriend;

var

  f : TFriend;

begin

  f := TFriend.Create;

  f.FCount := 0; // in C++ access to TLonely::FCount is not possible

...

 

DelphiXE2Cpp11 adds another line to  "friends_friends.inc"

 

friend class TFriend;

friend class TLonely;

friend void __fastcall NeedsFriend();

 

 

Now

 

void __fastcall NeedsFriend()

{

  TFriend* F = NULL;

  F = new TFriend();

  F->FCount = 0;

 

also compiles without problem.

 

 

 



This page belongs to the DelphiXE2Cpp11 Documentation

DelphiXE2Cpp11 home  Content