Recent

Author Topic: Analysis of some class. Seven questions.  (Read 3539 times)

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Analysis of some class. Seven questions.
« on: April 22, 2017, 03:53:12 pm »
We have some Class someClass.

How to know:

1. Line of create of this class.
someClass= class(BaseClass); or
someClass= class; or
...

2. name of BaseClass, if exists.

3. If has already memory (was initialized) or not still (in RunTime).

4. List of all Methods of class with section name (public, publish, ...) for each Method.

5. List of all Fields of class with section name (public, publish, ...) for each Field.

6. List of all Properties of class with section name (public, publish, ...) for each Property.

And  one additional question more:
7. How can be duplicate completely someClass to newSomeClass?
I saw several information in Internet about this question and question 6.
For duplicate it's used method
Clone
(http://stackoverflow.com/questions/4041760/correct-way-to-duplicate-delphi-object),
and method Assign
(http://stackoverflow.com/questions/23163655/can-i-use-assign-to-duplicate-an-object-of-objects)

But I do not know which is better.
« Last Edit: April 22, 2017, 03:58:47 pm by yurkad »

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Re: Analysis of some class. Seven questions.
« Reply #1 on: April 22, 2017, 07:02:21 pm »
I suggest you to check the TObject class from which so many classes are derived (except the ones you make yourself), and for example it implements this function to return the class as a string: http://freepascal.org/docs-html/current/rtl/system/tobject.classname.html

It has some other related functions related to class identification. Some descendant classes of TObject are also useful, such as TPersistent which has the virtual method Assign to make copies of objects.

For question 1: when you make a new class, you are just saying if you're using another class as ancestor, or not. With a base class, the new class will inherit all the characteristics of the base class: fields, methods, properties, and ancestors. Note that some base classes need certain methods to be implemented since they're declared but not implemented yet.

For question 2: answered above.

For question 3: don't confuse a class (a template which describes an object with fields and methods) and an instance of that class. So someClass = class(BaseClass) is a class declaration where you define the fields and methods and whatever; and somevar := someClass.Create will create and store an instance of that class inside the variable somevar. If you mix terminologies, you'll mix all the rest, both for you and for us to explain anything or to even understand your questions. Said that, if somevar = Nil then the variable doesn't contain an instance yet. If it's different from Nil, it might have been instantiated and stored there before, but you can't know the very moment you do that check if that instance (it's just a pointer to a memory place in the heap) is still valid or the instance was freed. That's why I only free local instance variables at the end of functions, in unit finalization blocks, or use FreeAndNil so I don't mix stuff. In short, if it's Nil it hasn't been created, if not Nil it was created but only you are responsible of knowing if that particular instance is valid through wise creation and freeing, or using indirect methods like storing the instances in collections which take care of freeing and nil-ing for you.

For questions 4 to 6: I'm guessing you mean everything a class has, including whatever has inherited from ancestor classes. I don't think there's a way to list that within Lazarus given a certain class, someone correct me if I'm wrong. The old slow way is to use alt+up and control+down while the cursor is above the class name to navigate between the declaration and implementation of everything. If you're at a class name e.g. TPersistent, alt+up will move you to its declaration. There you can move to the declaration of its ancestor or down to the fields and method declarations. Note that if you're in a method declaration and press again alt+up, and the ancestor class also has that method, the code editor will move you to the method declaration of the ancestor. At any method declaration, control+down will move you to the implementation. And of course there's the help, the online help, and the wiki.

For question 7: here you're mixing things as I've said before. It's not clear if you want to duplicate a class, or create a new instance of an already created instance which is an exact duplicate. Judging by the links you posted, it's the second option. As you might know, newobj := oldobj just duplicates the pointers, so both variables point to the same place in the heap. TPersistent has the Assign method, which is the standard name for duplication/cloning of instances; however that method must be implemented in each descendant class to copy each internal field from one instance to another. In other words, to clone an instance the class must have a method to copy each class field to the new instance similar to newobj.Assign(oldobj). Something like (if it comes from TObject and TPersistent, crude example):

Code: Pascal  [Select][+][-]
  1. procedure TSomeClass.Assign(const instance: TPersistent)
  2. var
  3.   i : TSomeClass;
  4.  
  5. begin
  6.   if instance is TSomeClass then
  7.   begin
  8.       i := TSomeClass(instance);
  9.       FFieldWhatever := i.FFieldWhatever;
  10.       FSomeOtherField := i.FSomeOtherField;
  11.       FImportantField := i.FImportantField ;
  12.   end;
  13. end;

Assign implementations in classes descending from TPersistent call the ancestor method in case the instance you want to clone is not of the same class, using 'inherited Assign(instance)' and they call the instance to clone 'source', but more or less that's about it.

Oh I have a pretty naive approach to objects. Others might have other answers.

One final thing: it's a tradition of Pascal to name all types starting with uppercase T (except interfaces and exceptions which start with I and E respectively). So I'd not name it someClass, but TSomeClass. Also, fields start with F (just the fields, not the properties). I know you only used it as an example, but if you're not naming your types and classes starting with T, you can start right now ;)
« Last Edit: April 22, 2017, 07:13:47 pm by jmm72 »
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Analysis of some class. Seven questions.
« Reply #2 on: April 22, 2017, 07:34:07 pm »
I suggest you to check the TObject class from which so many classes are derived (except the ones you make yourself)

All classes inherit from TObject.
The following definitions are equal:

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyClass = class
  3.   end;
  4.  
  5.   TMyClass = class(TObject)
  6.   end;
  7.  

Bart

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11450
  • FPC developer.
Re: Analysis of some class. Seven questions.
« Reply #3 on: April 22, 2017, 07:35:53 pm »

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: Analysis of some class. Seven questions.
« Reply #4 on: April 23, 2017, 12:59:27 am »
jmm72,  Bart,  marcov
many thanks!

jmm72, All information is very useful for me.
I understood almost all. But I have some question.
For question 7: here you're mixing things as I've said before. It's not clear if you want to duplicate a class, or create a new instance of an already created instance which is an exact duplicate.
I see difference between "duplicate a class" and "create a new instance of an already created instance" by following mode:
Sample:
We have code
Code: Pascal  [Select][+][-]
  1. Type
  2.   TSomeClass = class(TObject)
  3.   ...
  4.   end;
  5.   TNewSomeClass = class(TSomeClass)
  6. end;
  7.  
  8. Var
  9.   SomeClass:TSomeClass;
  10.   NewSomeClass:TNewSomeClass;

Here TSomeClass and TNewSomeClass haven same content = same struct = same definition.
Neither has values of RunTime.

During RunTime SomeClass and NewSomeClass are independent and can have different values of fields and properties.
Definitions are same but values are different.

But using TPersistent/Assign in RunTime applicated to variable SomeClass we can have another class NewSomeClass2 with same definition and same values. Same values directly after assign.

After this each class will have his own life and values can be different.

Is it Thus?



« Last Edit: April 23, 2017, 01:13:31 am by yurkad »

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Re: Analysis of some class. Seven questions.
« Reply #5 on: April 23, 2017, 10:00:33 pm »
All classes inherit from TObject.

Ahhh I knew something was amiss in my explanation but couldn't identify it. Well I hope I can learn (again) from my own errors; all this iloveprogramming/ihateprogramming makes me forget even the most basic things. Although my last hiatus was not for hating programming, but for lack of time and energy.

Thanks for the correction.
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

 

TinyPortal © 2005-2018