What's the difference between public and published class members in Delphi?
Asked Answered
C

8

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.

Couteau answered 1/7, 2010 at 12:18 Comment(0)
H
25

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

Haematoxylin answered 1/7, 2010 at 12:25 Comment(3)
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.Elegy
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.Ambala
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?Queston
O
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.

Ocotillo answered 1/7, 2010 at 12:21 Comment(5)
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.Territorialism
@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.Ocotillo
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.)Territorialism
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.Elegy
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 delphibasics.co.uk/RTL.asp?Name=Published Moreover, Published properties cannot return arrays.Stephanistephania
H
25

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

Haematoxylin answered 1/7, 2010 at 12:25 Comment(3)
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.Elegy
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.Ambala
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?Queston
C
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'.

Caesura answered 1/7, 2010 at 12:40 Comment(0)
T
4

Published properties will export Runtime Type Information (RTTI).

Have a look here about RTTI in Delphi

Tampon answered 1/7, 2010 at 12:24 Comment(0)
I
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.

Incrustation answered 1/7, 2010 at 13:27 Comment(0)
S
3

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

Squander answered 1/7, 2010 at 12:24 Comment(2)
Unless {$M+} / {$TYPEINFO ON} is specified, in this case public members will have RTTI tooCaesura
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.Submissive
T
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.

Tecu answered 1/7, 2010 at 12:35 Comment(0)
B
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.

Butadiene answered 1/7, 2010 at 12:40 Comment(1)
This is probably the most important distinction! RTTI is just some the 'glue' needed to make this happen.Celebrity

© 2022 - 2024 — McMap. All rights reserved.