Why Delphi/pascal builders cannot be private like in C++

Asked

Viewed 439 times

3

Is there any reason why I can’t leave a Delphi constructor as private?

  • How will you build the object if the constructor is private?

  • @Embarbosa, in C++ the visibility of the constructor can be private, and internally I see no reason for this feature not to be allowed in Delphi/pascal, this hinders a lot when we need to create a Singleton object, because the constructor is always exposed, of course we use tricks to circumvent it but it is by no means elegant.

  • I don’t know of a situation where this could get in the way of... But you may want to check this question and its answers here http://stackoverflow.com/a/5392736/460775

  • Ahh... now that I saw that I had already asked the question on the Soen and received basically the same answer. http://stackoverflow.com/q/36535704/460775

  • @Embarbosa, If you read the comments of my question in stackoverflow-en will come to the conclusion that there is no possibility of hiding builders in Delphi, you can do tricks using overloading, but the create method is always available, exactly what I questioned is why I cannot change the visibility of the constructor, I can do this in java, c++ but not in pascal. What I meant to say is that "disturbs", is that you have to use tricks to be able to make a Singleton, cool the example you posted but using interfaces and critical session?

  • @Embarbosa looks at C++: class Logger{ public: Static Logger* Instance(); bool openLogFile(Std::string logfile); void writeToLogFile(); bool closeLogFile(); private: Logger(){}; // Private so that it can not be called Logger(Logger const&){}; // copy constructor is private Logger& Operator=(Logger const&){}; // assignment Operator is private Static Logger* m_pInstance; };

Show 1 more comment

2 answers

1


Contrary to what you mentioned, you can declare a private constructor in Delphi. See, the code below simply works:

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Unit1 in 'Unit1.pas';

//var Foo: Tfoo2;
begin
  try
//    Foo := Tfoo2.CreatePrivado(1);
    TFoo2.FA := 'hello world';
    writeln(Foo.FA);
    readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

unit Unit1;

interface

type Tfoo2 = class(TObject)
     private
       constructor CreatePrivado(i:integer);
     public
       class var FA: string;
//       constructor Create(bar:Boolean);
     end;

implementation

uses
  SysUtils;

//constructor Tfoo2.Create(bar:Boolean);
//begin
//  inherited Create;
//  FA := 'bar';
//end;


constructor Tfoo2.CreatePrivado(i: integer);
begin
  inherited Create;
  FA := IntToStr(i);
end;

end.

In Delphi, constructors can be inherited. This does not happen in Java, C# or C++ for example. In addition, a class can have multiple constructors and they can also have different names. Usually called Create. But this is just a convention and not a rule.

Further, all classes in Delphi ultimately inherit from the class TObject. This class contains a constructor without parameters called Create.

Thus, it is easy to understand why all classes in Delphi have the constructor without parameters called Create.

If you need to hide the builder Create already mentioned, try an answer to one of the following questions on Soen:

https://stackoverflow.com/a/14003208/460775

https://stackoverflow.com/q/5392107/460775

https://stackoverflow.com/q/14003153/460775

Part of the answer is based on: http://www.yanniel.info/2011/08/hide-tobject-create-constructor-delphi.html

  • then test the example of your answer call create and it is still displaying example foo2 := Tfoo2.Create;

  • @Wellingtonsilvaribeiro I didn’t say I wasn’t. Did you read the links I passed? You’re mixing things up. One thing is builders can’t be private, another is you hide the Create constructor. What’s the point of your question?

  • my question is still about constructor visibility, some of them always gets vivid, my question is if there is any structural reason in Deli not to allow me to be prevented from calling create on the occasion, Your example actually compiles but what good it is if I have an invented constructor if I can still call create. I never said I didn’t know how to make a Singleton as it is in the links, my complaint is that in Delphi we have to use tricks to get a result that in java or c++ would be much simpler.

  • @I’m sorry Wellingtonsilvaribeiro, but I didn’t say you don’t know. I also didn’t say you don’t have to do anything. What I asked is that my answer already answers your question as is. I have nothing further to add on how the question is at the moment.

0

To be honest with you Delphi does have Private methods for a Builder yes, when a language is said to be Oriented it must follow the basis of the pillars of the POO (Encapsulation, Abstraction, Heritage and Polymorphism) I believe that for this you must declare before using the same

type Class declaration
...
Constructor Name; {Overload;}
...
end;

type Class declaration
...
Constructor Name(Arguments); {Overload;}
...
end;

I hope I’ve helped you at least a little :/

  • really the pillars of an object-oriented language is heritage, polymorphism, abstraction and encapsulation, however my question is about visibility you can not put a constructor in Delphi/pascal as private, in Tobject the constructor is not virtual. This peculiarity affects how you apply object-oriented design to the standards of known projects such as I cited in the question comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.