Recent

Author Topic: Classes vs Objects  (Read 702 times)

TBMan

  • Full Member
  • ***
  • Posts: 189
Classes vs Objects
« on: June 21, 2025, 01:58:50 am »
I'm a programming hobbyist. I'm used to using pointers to objects and I am comfortable working with them. What am I missing by not using classes?

The reason why I am asking is because with an object that "owns" another object, to have a child of the "owned" object work correctly I have to do a typecast to access the child object's variables and methods that are added. Here's what I'm talking about:

Code: Pascal  [Select][+][-]
  1. type
  2.   PLg_Taskbar=^LG_Taskbar;
  3. LG_Taskbar = object(baseobject);
  4.   constructor init;
  5.   procedure paint;virtual;
  6.   destructor done;
  7. end;
  8.  
  9. PLg_Application = ^LG_Application;
  10. LG_Application = Object(baseobject)
  11.   taskbar:PLG_Taskbar;
  12.   constructor buildapp;
  13.   procedure paint;virtual
  14.   destructor done;
  15. end;
  16.  
  17. PLG_NewTaskbar = ^LG_NewTaskbar;
  18. LG_NewTaskbar = object(LG_taskbar)
  19.    Showing:boolean;     // have to typecast to access
  20.    constructor buildnewtaskbar;
  21.    procedure paint;virtual;
  22.    procedure hide;virtual;  // have to typecast to access
  23. end;
  24.  
  25.  

So when I create a taskbar owned by the application the code I use is
Code: Pascal  [Select][+][-]
  1.   Taskbar := new(PLG_NewTaskbar,buildnewtaskbar);
  2.  

To access the new taskbar's hide method I have to do this
Code: Pascal  [Select][+][-]
  1. Plg_NewTaskbar(taskbar)^.Hide;
  2.  

Does this issue also happen with classes (and pointers to classes)?
« Last Edit: June 21, 2025, 02:27:11 am by TBMan »

ASerge

  • Hero Member
  • *****
  • Posts: 2436
Re: Classes vs Objects
« Reply #1 on: June 21, 2025, 02:33:32 am »
Does this issue also happen with classes (and pointers to classes)?
Yes, you need to typecast the type of the base class to an extended one in order to access the fields and methods of the extended class.
Classes are already pointers, so you don't usually make a pointer to a class for them. For recursion, you can make a forward declaration of a class without a specification.

TBMan

  • Full Member
  • ***
  • Posts: 189
Re: Classes vs Objects
« Reply #2 on: June 21, 2025, 02:43:06 am »
Does this issue also happen with classes (and pointers to classes)?
Yes, you need to typecast the type of the base class to an extended one in order to access the fields and methods of the extended class.
Classes are already pointers, so you don't usually make a pointer to a class for them. For recursion, you can make a forward declaration of a class without a specification.

Ok thank you!

cdbc

  • Hero Member
  • *****
  • Posts: 2247
    • http://www.cdbc.dk
Re: Classes vs Objects
« Reply #3 on: June 21, 2025, 10:09:25 am »
Hi
Hmmmm, you could try this:
Code: Pascal  [Select][+][-]
  1. type
  2.   { as long as it's in the same 'type-block', you should be able to
  3.      place the 'forward definition' HERE instead of below by the object }
  4.   PLG_NewTaskbar = ^LG_NewTaskbar;
  5.  
  6.   PLg_Taskbar=^LG_Taskbar;
  7. LG_Taskbar = object(baseobject);
  8.   constructor init;
  9.   procedure paint;virtual;
  10.   destructor done;
  11. end;
  12.  
  13. PLg_Application = ^LG_Application;
  14. LG_Application = Object(baseobject)
  15.   taskbar:PLG_NewTaskbar; /// <- change HERE ///
  16.   constructor buildapp;
  17.   procedure paint;virtual
  18.   destructor done;
  19. end;
  20.  
  21. LG_NewTaskbar = object(LG_taskbar)
  22.    Showing:boolean;     // have to typecast to access
  23.    constructor buildnewtaskbar;
  24.    procedure paint;virtual;
  25.    procedure hide;virtual;  // have to typecast to access
  26. end;
  27.  
If I'm right, this should do away with the typecasting...  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

jamie

  • Hero Member
  • *****
  • Posts: 6989
Re: Classes vs Objects
« Reply #4 on: June 21, 2025, 03:25:15 pm »
Objects still have their use.

 Static Objects that can be ready to go from the stack or heap as long as you don't put any virtuals in it.

  They don't support a few things which are not really a big deal at times if you can live with that, like Class methods etc.

 I do like using them for inheritance of Records features where I can stack a few base items and use it just for variables. They aren't real in the sense of multiple inheritance, but it allows you to create bases etc.

 IF advanced records allowed this it would be a god sent because we could then have Class items!

 Jamie


The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 17388
  • Ceterum censeo Trump esse delendam
Re: Classes vs Objects
« Reply #5 on: June 21, 2025, 03:41:55 pm »
You can have class items in objects:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$endif}
  2. type
  3.   TMyObject = object
  4.   class var x:integer;
  5.   end;
  6. var
  7.   a,b:TMyobject;
  8. begin
  9.   a.x:=100;    // class var
  10.   writeln(b.x);// class var, prints 100
  11. end.
Or is that not what you mean?
(This does not work in mode TP, though, unless you add {$modeswitch class})
« Last Edit: June 21, 2025, 04:51:44 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

TBMan

  • Full Member
  • ***
  • Posts: 189
Re: Classes vs Objects
« Reply #6 on: June 21, 2025, 08:15:51 pm »
Hi
Hmmmm, you could try this:
Code: Pascal  [Select][+][-]
  1. type
  2.   { as long as it's in the same 'type-block', you should be able to
  3.      place the 'forward definition' HERE instead of below by the object }
  4.   PLG_NewTaskbar = ^LG_NewTaskbar;
  5.  
  6.   PLg_Taskbar=^LG_Taskbar;
  7. LG_Taskbar = object(baseobject);
  8.   constructor init;
  9.   procedure paint;virtual;
  10.   destructor done;
  11. end;
  12.  
  13. PLg_Application = ^LG_Application;
  14. LG_Application = Object(baseobject)
  15.   taskbar:PLG_NewTaskbar; /// <- change HERE ///
  16.   constructor buildapp;
  17.   procedure paint;virtual
  18.   destructor done;
  19. end;
  20.  
  21. LG_NewTaskbar = object(LG_taskbar)
  22.    Showing:boolean;     // have to typecast to access
  23.    constructor buildnewtaskbar;
  24.    procedure paint;virtual;
  25.    procedure hide;virtual;  // have to typecast to access
  26. end;
  27.  
If I'm right, this should do away with the typecasting...  8-)
Regards Benny

That would probably work without breaking my old code. I'll test it out later. I can move the new taskbar to my library and change the application taskbar to the new one as you pointed out. Thanks Benny.

TBMan

  • Full Member
  • ***
  • Posts: 189
Re: Classes vs Objects
« Reply #7 on: June 21, 2025, 08:23:08 pm »
You can have class items in objects:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$endif}
  2. type
  3.   TMyObject = object
  4.   class var x:integer;
  5.   end;
  6. var
  7.   a,b:TMyobject;
  8. begin
  9.   a.x:=100;    // class var
  10.   writeln(b.x);// class var, prints 100
  11. end.
Or is that not what you mean?
(This does not work in mode TP, though, unless you add {$modeswitch class})

No, but thanks for taking the time to respond. I'll have to play around with classes to see what they can do. There's a lot of the newer "stuff" that I don't know.

TBMan

  • Full Member
  • ***
  • Posts: 189
Re: Classes vs Objects
« Reply #8 on: June 22, 2025, 04:34:59 pm »
Hi
Hmmmm, you could try this:
Code: Pascal  [Select][+][-]
  1. type
  2.   { as long as it's in the same 'type-block', you should be able to
  3.      place the 'forward definition' HERE instead of below by the object }
  4.   PLG_NewTaskbar = ^LG_NewTaskbar;
  5.  
  6.   PLg_Taskbar=^LG_Taskbar;
  7. LG_Taskbar = object(baseobject);
  8.   constructor init;
  9.   procedure paint;virtual;
  10.   destructor done;
  11. end;
  12.  
  13. PLg_Application = ^LG_Application;
  14. LG_Application = Object(baseobject)
  15.   taskbar:PLG_NewTaskbar; /// <- change HERE ///
  16.   constructor buildapp;
  17.   procedure paint;virtual
  18.   destructor done;
  19. end;
  20.  
  21. LG_NewTaskbar = object(LG_taskbar)
  22.    Showing:boolean;     // have to typecast to access
  23.    constructor buildnewtaskbar;
  24.    procedure paint;virtual;
  25.    procedure hide;virtual;  // have to typecast to access
  26. end;
  27.  
If I'm right, this should do away with the typecasting...  8-)
Regards Benny

That would probably work without breaking my old code. I'll test it out later. I can move the new taskbar to my library and change the application taskbar to the new one as you pointed out. Thanks Benny.

No, it was a code breaker. I couldn't use an ancestor constructor with the new taskbar.

 

TinyPortal © 2005-2018