33

Please could someone explain me what's the difference between public and published class members in Delphi?

I tried to look at Delphi help and I understand that these members have the same visibility, but I don't understand very well how they differ and when should I use published members instead of public ones.

Thanks a lot.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Ondra C.
  • 2,492
  • 3
  • 33
  • 35

8 Answers8

31

The compiler generates RTTI (Run-Time Type Information) metadata for published members, but not for public members (by default). The main effect of this is that the published properties of an object will appear in the Object Inspector at design time.

I do not know if you are writing components, but if you do, you probably know that properties and events are normally published, so that they can be set using the Object Inspector.

Public

public
  property MyProperty: integer read FMyProperty write FMyProperty

MyProperty will not be visible in the Object Inspector.

Published

published
  property MyProperty: integer read FMyProperty write FMyProperty

MyProperty will be visible in the Object Inspector.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 3
    While this is correct, the underlying cause for why it appears in the object inspector is that RTTI metadata is present for published members. In other words, what you're seeing is one effect of the underlying difference. A more correct answer is that published members have RTTI, while public members have not. – Lasse V. Karlsen Jul 01 '10 at 12:27
  • 1
    @Lasse V. Karlsen: Yes, you're right. But since this is probably the most visible difference (?), and no other answer have pointed it out yet, I will refrain from removing my answer right now. – Andreas Rejbrand Jul 01 '10 at 12:30
  • 1
    Sorry, I didn't make myself clear enough :) I agree, this is the most observable effect of the difference. My point was that you should point out that the underlying difference is RTTI, not remove your answer (even though other answers are pointing to RTTI now.) – Lasse V. Karlsen Jul 01 '10 at 12:37
  • 3
    I think its worth pointing out however that if you are asking a question regarding the difference between Public and Published methods then the term RTTI probably wont mean anything to you either. Andreas has provided a very useful answer. – Toby Allen Jul 01 '10 at 23:04
  • But there is a difference for methods. And it was not mentioned in this answer. At least this one: "Only one Constructor may be declared as published - overloaded versions must be defined as public." from here http://www.delphibasics.co.uk/RTL.asp?Name=Published Moreover, Published properties cannot return arrays. – Yevgeniy Afanasyev Dec 09 '14 at 22:40
25

Public properties and published properties have the same visibility, as you already stated. Published properties are included in RTTI, public properties aren't.

R-D
  • 1,154
  • 1
  • 10
  • 25
  • 12
    RTTI stands for Run Time Type Information or the information that the Delphi Compiler needs at Design time to make the object inspector and other parts of the Delphi IDE work correctly at design time. – Toby Allen Jul 01 '10 at 23:07
  • 4
    For completeness: Delphi 2010 can generate RTTI for public members as well, in fact in can generate RTTI for all members, including protected and private. But published members are still the ones "published" by the Object Inspector and used by the automatic streaming mechanism. – Cosmin Prund Jul 02 '10 at 06:33
  • Does Free Pascal work like Delphi 2010? As in, does it generate RTTI for public properties? Would it be safe to not use published for simplicity? – jocull Mar 10 '18 at 02:19
9

As a side note, there is another special thing with published:

The default visibility of class members is published, so check for unsafe code like:

  TTopSecret = class(TObject)
    Name: string;
    Password: string;

    function DecryptPassword(const AValue): string;  
  public
    constructor Create(const AName, AEncryptedPassword: string);
  end; 

Name, Password and DecryptPassword() are visible 'world-wide'.

mjn
  • 36,362
  • 28
  • 176
  • 378
4

Published properties will export Runtime Type Information (RTTI).

Have a look here about RTTI in Delphi

Community
  • 1
  • 1
Ray
  • 45,695
  • 27
  • 126
  • 169
4

It seems there are lots of good answers already, pointing out the Object INspector, RTTI, etc. These are all pieces of the puzzle.

If you take away the published keyword, the entire Delphi RAD tool design would require some way to specify which properties are stored in a DFM, inspected in the component property inspector, and can be reloaded at runtime from a DFM when the form or data module is created.

This, in a word, is what Published is for. It is interesting to me that the designers of QT (originally TrollTech, later part of Nokia, later still spun off to Digia) had to emulate this level of RTTI for their C++ RAD library "QT", adding a "published" equivalent and a "property" equivalent, while pure C++ still lacks this fundamental facility.

Warren P
  • 65,725
  • 40
  • 181
  • 316
3

Runtime Type Informations (RTTI) are only generated for published class members.

splash
  • 13,037
  • 1
  • 44
  • 67
  • Unless {$M+} / {$TYPEINFO ON} is specified, in this case public members will have RTTI too – mjn Jul 01 '10 at 12:31
  • If that's true, @Mjustin, then it is a change from earlier Delphi versions. It used to be that *without* $M+, public and published were equivalent. That is, the published section did not generate RTTI unless $M+ was in effect. – Rob Kennedy Jul 01 '10 at 14:12
3

At run-time, entries in the published and public sections are equally accessible.

The principal difference between them is that published items of a component appear in the Object Inspector at design-time.

This happens because, for fields in published section RTTI is automatically generated.

The Object Inspector picks this up and uses it to identify what to add to its list of properties and events.

Bharat
  • 6,828
  • 5
  • 35
  • 56
3

In addition to the other answers:

Published properties are automatically stored by the streaming system.

For instance if you have a TComponent's descendant instance and write it to a TStream with WriteComponent, all (well, not all, but that is another question) published properties are written to the stream without any further coding.

Of course, the streaming system only can do that because the RTTI is available for those published properties.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • This is probably the most important distinction! RTTI is just some the 'glue' needed to make this happen. – Roddy Nov 20 '14 at 12:08