Jump to content

SgtZdog

Members
  • Content Count

    7
  • Joined

  • Last visited

Everything posted by SgtZdog

  1. Hey all, I'm a rather new to Delphi (a few years programming in C#, C++, Python, and Clojure). (A note, the original thought that brought up this question is abandoned as I thought of a somewhat better solution for when I was originally writing this question, but the core question still seems good to have an answer for,) I'm trying to have a static method in a child class access protected members declared in the parent, but having trouble figuring out how to do it. Is it not possible or am I doing it wrong? Simplified code example: edit: I tried making the method I want to use owned by the child class instead (parent class doesn't particularly need to own it anyway), but that doesn't seem to work either. unit1.pas: interface type TParentClass = class protected function ProtectedMember(); Boolean; virtual; abstract; unit2.pas interface type ChildClass = class(ParentClass) public //Question 1) class function Maker() : ChildClass; //Question 2) (I haven't tested this, so maybe this works no problem?) class procedure Manipulator(instance : ParentClass); //Question 3) (I haven't tested this, so maybe this works no problem?) procedure Stuffer(instance2 : ParentClass); protected procedure ChildOwnedMethod(); end; implementation class function ChildClass.Maker() : ChildClass; begin Result := ChildClass.Create(); //Edit: No longer incorrectly use inherited Result.ProtectedMember(); //Trying to do this and it doesn't seem to work Result.ChildOwnedMethod(); //Bonus! I tried doing this instead and it doesn't seem to work either. end; class procedure ChildClass.Manipulator(instance : ParentClass); begin instance.ProtectedMember(); //Does this work? Is there a way to do it? end; procedure ChildClass.Stuffer(instance2 : ParentClass); begin ProtectedMember(); //I would expect this to work instance2.ProtectedMember(); //Does this work though? Is there a way to do it? end; procedure ChildClass.ChildOwnedMethod(); begin //Do Stuff end;
  2. Messages aren't really processed by the log, just held by it, barring the filtering provided by the different GetLogEntries methods. Pretty much anywhere in the application can read/write the log (note that we functionally only have a few places that read from it, but many that write to it). The messages are objects that I'm currently experimenting with using some inheritance to simplify things sort of since we have over 100 different possible message formats. The application is broadly speaking single-threaded and not thread safe. edit: The log is a global right now, and fixing it up to be singleton is probably on my todo list.
  3. The class is nothing terribly complex, but here's the interface with the core functions (for brevity I'm not including everything like I/O for saving and loading): TMessageLog = class public constructor Create(); destructor Destroy(); override; procedure Clear(); procedure AddEntry(aLogEntry : TMessageLogEntry); function GetLogEntries(reporter : Integer) : TArray<TMessageLogEntry>; overload; function GetLogEntries(reporter : Integer; anUntilTime : TDateTime) : TArray<TMessageLogEntry>; overload; function GetLogEntriesMatchingAnyTag(reporter : Integer; const theTags : TArray<TMessageLogTag>) : TArray<TMessageLogEntry>; overload; end; (I'll note, I am not the original author of this class and it is still subject to change)
  4. Yes, there is a separate class not described by my current efforts that holds the messages and deals with displaying them. This is for the messages themselves. So the entire log is held by one class, with each message being an instance of something that inherits from the parent class here. Our legacy system messages were plain strings and then some regex would let us add functionality. I'm experimenting with moving away from these plain strings for speed, ease of use, and capability purposes. MessageClass / \ type1 type2 Log: contains array of MessageClass
  5. Programmer, I definitely thought about virtual, but the current base class has no need for the virtual methods and under my current concept construction of the base class would be replaced by more specific child classes. (I'm assuming your use of properties was just for demonstration purposes and not part of a proposed solution.)
  6. I was indeed going for something like a factory pattern (though simplified in that each child class provides static methods for construction instead of having a separate class for the sole purpose of construction). Rest assured, as mentioned above, I'm reducing a bunch of this. Reason for the factory style idea though was to return nil instead of raising an exception in the case of bad construction. I might still keep the static construction methods, but they will at least avoid the unnecessary polymorphism. The current task I'm working on is overhauling a logging system for a game (these are user facing reports). The messages have quite complicated construction and I'm trying to test the viability of removing the message generating from the game logic (this is beneficial in that it allows us to more easily and dynamically have actions like highlighting given units in the messages).
  7. Ah, this was a good catch: Yeah, that was not intended and was my mistake. I fixed up the original post to do things correctly. Things seem to be working now, thank you very much for your help. To hopefully answer the core of your questions, the intent of the protected abstract method is to enforce child classes having to have it so that a parent method can call it and lean on a correct implementation being available courtesy polymorphism. As I mentioned I've changed tack, so this isn't what I'm going to do, but this was still good practice at Delphi syntax for me.
×