Circular reference lazarus / 61-Beginners ---------------------------------------------------------------------- Bill52 · 2014-09-01 12:19 I have two classes: ClassA and ClassB. ClassA stores a list of pointers to ClassB. ClassB stores a pointer to ClassA. Both classes have public functions/procedures to set/get the pointers. IE: can't move the 'uses' clause to the implementation. The classes have nothing in common.  It is a 'has-a' not an 'is-a' relation. IE: inheritance doesn't apply. How do i go about resolving the circular reference? Thanks, bill ---------------------------------------------------------------------- Blaazen · 2014-09-01 12:30 There exists "forward declaration": Code: [Select]ClassB = class; ClassA = class ...//references to ClassB end; ClassB = class ...//references to ClassA end; But, do I understand well, your classes are in different units? EDIT: I don't see other way than moving classes to the same unit. ---------------------------------------------------------------------- Bill52 · 2014-09-01 12:48 Yes, they are in separate units. Each are some 150 lines. Moving them to one unit would be awful. ---------------------------------------------------------------------- Blaazen · 2014-09-01 13:00 You can use references to - for example - TObject (in declaration) and use casting in implementation, i.e.: Code: [Select]with ReferenceToTObject as ClassB do   begin   end; which will allow you to move one of the units to the 'uses' clause to the implementation. But it seems to me much more work than just CTRL+C and CTRL+V the classes to the same unit. ---------------------------------------------------------------------- taazz · 2014-09-01 13:37 Well 150 lines of code is not that big and there is nothing awful of having all the related classes in one unit it is called encapsulation. Other than what blaazen told you, you can have a 3rd unit where the abstract classes of those two are defined and that unit is used from the class implementation units. ---------------------------------------------------------------------- marcov · 2014-09-01 15:02 Or separate one class into a minimal base class (what the other needs) and derive it for the rest of the implementation. Code: [Select]unita: type      classb= class;       baseclassa = class         // references classb         end               classb = class                 // references baseclassa             end;                       unitb:      classa=class(baseclassa)                // rest of classa            end; ---------------------------------------------------------------------- circular · 2014-09-01 18:26 Talking about me? All suggestions seem fine: - using TObject for the parameters of the functions - using a base class for A and/or for B - merging into one unit ---------------------------------------------------------------------- hinst · 2014-09-01 20:18 moving unit dependency to implementation section of the unit and then using Pointer or TObject type for argument is a bad idea in my opinion. First, TObject type does not say much about type of argument, so you will have to deduce what kind of argument is expected every time you insert a call to that method somewhere or remember it so you should have a good memory or you can put a comment like // pass only TSpecificType here Well, it's unsafe Second reason not to do this, and probably more important: FreePascal compiler sometimes has problems with units referencing each other, so it might start throwing runtime errors. It happened to me. I had project containing about 20 units in total, and some of them referenced each other in implementation forming some kind of circle. At some point FPC refused to compile it. Not sure if it was fixed later. I decided to eliminate circular references completely at that time, however there was another option: I noticed that if I clear build files each time, it could still compile my project, but I decided not to rely on it since it still could stop working completely at some point and also if you always clear compiled files they take more time to compile; well you get the idea ---------------------------------------------------------------------- skalogryz · 2014-09-02 01:33 Quote from: hinst on September 01, 2014, 10:18:01 pmmoving unit dependency to implementation section of the unit and then using Pointer or TObject type for argument is a bad idea in my opinion. First, TObject type does not say much about type of argument, so you will have to deduce what kind of argument is expected every time you insert a call to that method somewhere or remember it so you should have a good memory or you can put a comment like // pass only TSpecificType here Well, it's unsafe using Pointer is unsafe. using TObject is safe and it says enough about itself. Code: [Select]procedure SomeCallBack( obj: TObject); var   sp : TSpecificType; begin   if not (obj is TSpecificType) then Exit;   sp:=TSpecificType(obj);   ... end; ---------------------------------------------------------------------- Leledumbo · 2014-09-02 08:59 150 lines are short (anybody wants to open packages/univint/Movies.pas?). The use of units are to group things of the same realm, so it's correct to group related classes into one unit. If you think it's cumbersome to maintain due to long lines, use include files. The compiler and IDE has full support for include files, classes from different include files will still be part of the same unit. ---------------------------------------------------------------------- taazz · 2014-09-02 09:36 Quote from: Leledumbo on September 02, 2014, 10:59:17 amIf you think it's cumbersome to maintain due to long lines, use include files. The problem with the include files (and my main obstacle to start using C) is that for each component you need to create 2 include files one for interface and one for implementation splitting the code in to more files than using two units, it is a PITA. I think that include files is correctly used in delphi where they are used for minor support issue although lazarus has inbuild support for inc file and can auto jump to the correct file I wouldn't call that full support just abuse. ---------------------------------------------------------------------- howardpc · 2014-09-02 09:50 Quote from: taazz on September 02, 2014, 11:36:28 amThe problem with the include files (and my main obstacle to start using C) is that for each component you need to create 2 include files one for interface and one for implementation splitting the code in to more files than using two units, it is a PITA. An oft-seen LCL convention is to keep all related class declarations together in the interface of the "main group" unit, and then have an implementation section that lists {$I xxx.inc} statements corresponding to each of the class declarations from the interface above. This means you (almost) have a one-to-one class/disk-file relationship when it comes to maintenance. It seems a good compromise. ---------------------------------------------------------------------- taazz · 2014-09-02 09:54 Quote from: howardpc on September 02, 2014, 11:50:36 amQuote from: taazz on September 02, 2014, 11:36:28 amThe problem with the include files (and my main obstacle to start using C) is that for each component you need to create 2 include files one for interface and one for implementation splitting the code in to more files than using two units, it is a PITA. An oft-seen LCL convention is to keep all related class declarations together in the interface of the "main group" unit, and then have an implementation section that lists {$I xxx.inc} statements corresponding to each of the class declarations from the interface above. This means you (almost) have a one-to-one class/disk-file relationship when it comes to maintenance. I know and for that reason I hate delving in the lcl code and avoid if I can. One of pascal's strengths is encapsulation(ee all relevant code in one unit) include files as used in lcl brakes this convention and I always end up using some kind of grep to find what I'm looking for instead of the editor. ---------------------------------------------------------------------- Edson · 2014-09-02 15:50 I think using include files, is the best option, when having related classes on the interface section and  a lot of code for implementation. What I do is to try to separate the part of code, that is not going to change frequently, in the included file. This leave me the a small code and easier to maintain. I know, this break the encapsulation in some way, but it avoid to have many thousands of code on one single file. ---------------------------------------------------------------------- Leledumbo · 2014-09-02 16:43 Quote from: taazz on September 02, 2014, 11:36:28 amThe problem with the include files (and my main obstacle to start using C) is that for each component you need to create 2 include files one for interface and one for implementation splitting the code in to more files than using two units, it is a PITA. Look at windows related API implementation in the rtl. they only need one include file If you can't find, this is the way to do it: Code: [Select]// file.inc {$ifdef read_interface} here goes interface code {$else read_interface} here goes implementation code {$endif read_interface} then: Code: [Select]unit x; interface {$define read_interface} {$i file.inc} implementation {$undef read_interface} {$i file.inc} end. Include files do NOT break encapsulation in anyway, if we're talking about the same term. Unless you have your own definition, you have to be clear about that first. ---------------------------------------------------------------------- taazz · 2014-09-02 17:22 Quote from: Leledumbo on September 02, 2014, 06:43:16 pmQuote from: taazz on September 02, 2014, 11:36:28 amThe problem with the include files (and my main obstacle to start using C) is that for each component you need to create 2 include files one for interface and one for implementation splitting the code in to more files than using two units, it is a PITA. Look at windows related API implementation in the rtl. they only need one include file If you can't find, this is the way to do it: Code: [Select]// file.inc {$ifdef read_interface} here goes interface code {$else read_interface} here goes implementation code {$endif read_interface} then: Code: [Select]unit x; interface {$define read_interface} {$i file.inc} implementation {$undef read_interface} {$i file.inc} end. Include files do NOT break encapsulation in anyway, if we're talking about the same term. Unless you have your own definition, you have to be clear about that first. Encapsulation is the ability to have all related parts in one container it is mostly used in context with classes/objects but the container can be anything and in this cases I use the unit file (.pas) as the container. In that regard it brakes encapsulation having the code in multiple files. ---------------------------------------------------------------------- Leledumbo · 2014-09-03 00:15 Quote from: taazz on September 02, 2014, 07:22:02 pmEncapsulation is the ability to have all related parts in one container it is mostly used in context with classes/objects but the container can be anything and in this cases I use the unit file (.pas) as the container. In that regard it brakes encapsulation having the code in multiple files. Now that's clear. Your scope is files, my scope is modules/units. Certainly different POV. ---------------------------------------------------------------------- taazz · 2014-09-03 00:55 Quote from: Leledumbo on September 03, 2014, 02:15:59 amNow that's clear. Your scope is files, my scope is modules/units. Certainly different POV. One of the strengths that pascal has is that a unit is a single file instead of 2 that c has (.h, .c) breaking it out in multiple files you loose that advantage making it harder to work with. ---------------------------------------------------------------------- Bill52 · 2014-09-04 14:07 I am but a beginner in FreePascal. Please correct where necessary. I see a number of issues in the replies. - Encapsulation. It is not "the ability to have all related parts in one container". It is about restricting access to data and functions that work on that data. ('functions' used in a general 'working' sense) Sure, they are related because the functions work on that data. However, the emphasis is on hiding. - Terminology seems to be used very loose. The term 'File' shouldn't be used when talking about the program structure. A standard 'Unit' is a container providing encapsulation. It may even be looked like a 'singleton' which is a class that has exactly one instance. IE: not a 'file'. Has the same filename though. A unit containing a single class: Here the class provides the encapsulation, not the unit. It is still not a 'file' though. From program structure point of view 'file' doesn't apply. File applies to units if looking the unit from the OS's file-system point of view. - Putting several classes into a single unit is not encapsulation. In this case the unit is just a 'container'. (sort of ... ) - If a class's functions are distributed between different units, it is not braking the encapsulation. May be poor practice though. - Making functions/data public that shouldn't be is breaking the encapsulation even if it is in a class. Distributing code - that should be hidden - into several standard units  is breaking encapsulation. - Encapsulation does not apply to the original question. The question was about cross-referencing between two unrelated classes. (Relation meant in the sense of inheritance. Naturally, they are 'related' in the sense that they work towards the same goal in the same program) - Putting unrelated classes into the same unit. Consider this: A Car 'is-a' Vehicle. A Truck 'is-a' Vehicle. Both Car and Track inherits from Vehicle. A Car 'has-a' Engine. The Truck 'has-a' Engine. Both Car and Truck needs to know about it's engine. Also, the Engines have to know to which Car/Truck they belong to. We have now two cross-references. Are we going to put all the Vehicle-derivatives AND ALL THEIR SHARED PARTS into the same unit? Do I understand correctly, this is the official way? Or everything is a TObject and casting endlessly ... Thanks! Cheers! ---------------------------------------------------------------------- minesadorada · 2014-09-04 14:18 Quote from: Blaazen on September 01, 2014, 02:30:18 pmThere exists "forward declaration": Code: [Select]ClassB = class; ClassA = class ...//references to ClassB end; ClassB = class ...//references to ClassA end; I was reading my Delphi3 manual the other day (yes, I know..) and came across this which solved a minor issue whereby I wanted the 'main' class to be listed first in a component .. then I come across this post.  Class forward declaration is quite an 'obscure' feature and I'm glad Lazarus includes it. ---------------------------------------------------------------------- taazz · 2014-09-04 15:23 http://en.wikipedia.org/wiki/Encapsulation_%28object- oriented_programming%29 The above definition seems to contradict with you. ---------------------------------------------------------------------- eny · 2014-09-05 17:22 Quote from: taazz on September 04, 2014, 05:23:46 pmhttp://en.wikipedia.org/wiki/Encapsulation_%28object- oriented_programming%29 Unfortunately not a very good article. ---------------------------------------------------------------------- taazz · 2014-09-05 17:30 Quote from: eny on September 05, 2014, 07:22:54 pmQuote from: taazz on September 04, 2014, 05:23:46 pmhttp://en.wikipedia.org/wiki/Encapsulation_%28object- oriented_programming%29 Unfortunately not a very good article. the main thing to take from it is that encapsulation is used in two different ways one of them in the academic community and the other from user outside the academia. I find that both their merit and stick to simpler meaning when ever I can it is easier to combine them in to concrete sentences. ---------------------------------------------------------------------- marcov · 2014-09-05 22:56 Quote from: Bill52 on September 04, 2014, 04:07:23 pm- Encapsulation. It is not "the ability to have all related parts in one container". It is about restricting access to data and functions that work on that data. ('functions' used in a general 'working' sense) Sure, they are related because the functions work on that data. However, the emphasis is on hiding. That is information hiding, not encapsulation. OTOH sometimes they are named as principle and implementation. Yet another time, encapsulation is adding another object as hidden field of  an object and exposing them. I would say combining code and data is instantiation, but also here there are many conflicting sources. ---------------------------------------------------------------------- Leledumbo · 2014-09-06 04:51 I know some institutions teach encapsulation and information hiding as the same concept, but in fact it is not, though closely related. Encapsulation CAN BE an implementation of information hiding, but IS ALSO a concept of bundling data with its related operations. Encapsulation is a language feature, information hiding is a design decision. You can create a unit with publicly accessible variables, with all functions accessing the variables are publicly accessible as well. No information hiding is done. But you can decide not to expose the variables at all and only expose certain function that outer world (of the unit) need to know and may access. ---------------------------------------------------------------------- Bill52 · 2014-09-10 11:53 Quote from: eny on September 05, 2014, 07:22:54 pmQuote from: taazz on September 04, 2014, 05:23:46 pmhttp://en.wikipedia.org/wiki/Encapsulation_%28object- oriented_programming%29 Unfortunately not a very good article. There is a reason why universities don't accept Wikipedia as a source. Schildt, H., "The complete reference C++" fourth edition, Osborne. "Encapsulation is a mechanism that binds together code and the data that it manipulates, and keeps both safe from outside interference and misuse. In an object oriented language, code and data may be combined in such a way that a self contained 'black box' is created." Haven't seen any mention of 'academic community vs. outside of academic community'. ---------------------------------------------------------------------- taazz · 2014-09-10 17:57 Quote from: Bill52 on September 10, 2014, 01:53:58 pmQuote from: eny on September 05, 2014, 07:22:54 pmQuote from: taazz on September 04, 2014, 05:23:46 pmhttp://en.wikipedia.org/wiki/Encapsulation_%28object- oriented_programming%29 Unfortunately not a very good article. There is a reason why universities don't accept Wikipedia as a source. Schildt, H., "The complete reference C++" fourth edition, Osborne. "Encapsulation is a mechanism that binds together code and the data that it manipulates, and keeps both safe from outside interference and misuse. In an object oriented language, code and data may be combined in such a way that a self contained 'black box' is created." Haven't seen any mention of 'academic community vs. outside of academic community'. My experience in the field agrees with the wikipedia if that comes in conflict with the academic definition of the word then it simple means that wikipedia is right. ---------------------------------------------------------------------- tparry_venture · 2015-08-11 13:38 I too need to solve this has a part type of issue as the original poster. Has there been any resolution? In my case one of the files is very large and all I want to do is remember the pointer to it but all I get is the circular reference file issue. If I change to use TObject to save then everywhere I use that I have to cast it to the correct class. So unless there is some way to get around this issue we have to do the type casting? Or is there some shortcut I may not know about? ---------------------------------------------------------------------- eny · 2015-08-11 13:43 Try to use the New Topic button and describe your problem/question. http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way _to_ask_questions_in_the_forum.3F