Recent

Author Topic: Circular reference  (Read 21939 times)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Circular reference
« Reply #15 on: September 02, 2014, 07:22:02 pm »
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.
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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Circular reference
« Reply #16 on: September 03, 2014, 02:15:59 am »
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.
Now that's clear. Your scope is files, my scope is modules/units. Certainly different POV.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Circular reference
« Reply #17 on: September 03, 2014, 02:55:53 am »
Now 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.

Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Bill52

  • New member
  • *
  • Posts: 8
Re: Circular reference
« Reply #18 on: September 04, 2014, 04:07:23 pm »
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

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Circular reference
« Reply #19 on: September 04, 2014, 04:18:10 pm »
There 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.
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Circular reference
« Reply #20 on: September 04, 2014, 05:23:46 pm »
http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29

The above definition seems to contradict with you.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

eny

  • Hero Member
  • *****
  • Posts: 1634
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Circular reference
« Reply #22 on: September 05, 2014, 07:30:31 pm »
http://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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Circular reference
« Reply #23 on: September 06, 2014, 12:56:20 am »
- 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.

« Last Edit: September 06, 2014, 01:09:27 am by marcov »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Circular reference
« Reply #24 on: September 06, 2014, 06:51:09 am »
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

  • New member
  • *
  • Posts: 8
Re: Circular reference
« Reply #25 on: September 10, 2014, 01:53:58 pm »
http://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

  • Hero Member
  • *****
  • Posts: 5368
Re: Circular reference
« Reply #26 on: September 10, 2014, 07:57:40 pm »
http://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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

tparry_venture

  • New member
  • *
  • Posts: 9
Re: Circular reference
« Reply #27 on: August 11, 2015, 03:38:58 pm »
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

  • Hero Member
  • *****
  • Posts: 1634
Re: Circular reference
« Reply #28 on: August 11, 2015, 03:43:55 pm »
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

 

TinyPortal © 2005-2018