Recent

Author Topic: C++ Structs with inheritance, trying to sub it with something else.  (Read 1082 times)

jamie

  • Hero Member
  • *****
  • Posts: 6556
I have been porting over a rather large number of C++ files over to pascal and I ran into an issue where I learned that C++ has structs like advanced records. No Problem since Fpc has them with their operators etc.


 But then, I found out C++ can inherit from other structs? Ok, that became a problem, so I used OBJECTS instead and that worked just dandy!

 Then I found a C++ struct that was inheriting other structs along with Operator overloads, that is when the bricks hit the windows!

 So, I have now encapsulated Objects that can inherit inside Records so I can have my operators. This has become a nightmare to manage.

 is there any plans to have either or? Objects that can do operators or Records that can inherit ?

The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11768
  • FPC developer.
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #1 on: August 10, 2024, 03:55:59 pm »
Afaik there are no plans to add another object model beside normal classes. TP Objects are a legacy feature.


jamie

  • Hero Member
  • *****
  • Posts: 6556
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #2 on: August 10, 2024, 04:14:27 pm »
Ok, so Object is old, they still work and allow inheritance along with other expected benefits of methods and properties.

Are there any plans to allow Records to inherited?

Until this happens, Objects at this point must stay alive so I can duplicate C++ to a degree.

Btw, I tried the hack on Delphi, and it does seem to work there, too. Although like here, they discourage their use.

I find it interesting how the C++ language decided to implement inheritance like that of Class in Object Pascal, instead of using the multiple inheritance model. They must have seen some light at the end of the tunnel! :o


The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 15647
  • Censorship about opinions does not belong here.
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #3 on: August 10, 2024, 04:21:09 pm »
Yes, objects would work, I have done that in different context.
What also works is creating a pure abstract class and inherit from that into a new pure abstract class. You can subsequently hardcast to a (C++) external class.
Does not always work - it works with ASIO -. Make sure about the alignment and the calling convention, though.

To do it right is well beyond beginners, though.
You may need to adjust for the VMT address if it does not start at relative zero.

I experimented with that back in the days with the late Rudy Velthuis and these experiments are still on-line afaik. We had quite a bit of fun to make it work. The pure abstract idea is not mine, it is Rudy's.
What also works is a record that encapsulates multiple records:
records start at relative offset zero. You can mimic inheritance by encapsulation the record to expand inside a meta record and add records as you please)
« Last Edit: August 10, 2024, 04:34:47 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11768
  • FPC developer.
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #4 on: August 10, 2024, 04:30:08 pm »
Ok, so Object is old, they still work and allow inheritance along with other expected benefits of methods and properties.

Are there any plans to allow Records to inherited?

Afaik there are no plans to add another object model besides classes. Of course sometimes new Delphi features cross such plans, so it might change in the future.

For non speed dependent stuff you might use interfaces.

Thaddy

  • Hero Member
  • *****
  • Posts: 15647
  • Censorship about opinions does not belong here.
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #5 on: August 10, 2024, 04:46:49 pm »
For non speed dependent stuff you might use interfaces.
Interfaces are not necessarily slow compared to virtual methods.
Static methods is a different matter.
The construct I described above allows for record inheritance, through simply adding another record to the meta or outer record.
If I smell bad code it usually is bad code and that includes my own code.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11768
  • FPC developer.
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #6 on: August 10, 2024, 05:07:22 pm »
For non speed dependent stuff you might use interfaces.
Interfaces are not necessarily slow compared to virtual methods.

It is the automated memory management that is slow, not the call overhead. But it is all relative of course.

jamie

  • Hero Member
  • *****
  • Posts: 6556
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #7 on: August 10, 2024, 05:33:24 pm »
Interfaces do not solve the problem here.

Anyways, thanks for taking time to think about.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 15647
  • Censorship about opinions does not belong here.
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #8 on: August 10, 2024, 06:08:01 pm »
Interfaces do not solve the problem here.

Anyways, thanks for taking time to think about.
Well, actually *it does* solve the problem, in the same way that a pure abstract class helps, you can simply define a - corba - interface and use it as hardcast to an opaque pointer. Since the VMT starts at zero. There are lot's of things to play with. Just experiment and you will find out all those things for yourself.
That is actually one of the reasons I introduced the Opaque stuff in system.
( and was accepted )
Both a pure virtual class or a Corba interface - which are basically the same - can give you access to the memory layout of a C++ class and call its methods, provided you know the alignment, the calling convention and you know the VMT table address on the C++ side. Which is usually at zero, but not always.
This is also the only technique that I know of, where a Corba interface is actually useful.
« Last Edit: August 10, 2024, 06:20:48 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

JdeHaan

  • Full Member
  • ***
  • Posts: 137
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #9 on: August 10, 2024, 06:57:14 pm »
Jamie, not sure if this will help you but it might give you ideas.
Some time ago I translated a compiler written in C to FPC. The original book author is Bob Nystrom (http://craftinginterpreters.com).

In the below GitHub is a file named object.pas. The basics for object management are taken from: http://craftinginterpreters.com/strings.html#values-and-objects

The idea is that we can simulate object inheritance by using plain records.
Each record type 'derives' from a record TObj. It involves quite some memory management, however it does work.

https://github.com/jdehaan2014/fplox/tree/main

I didn’t use advanced records at the time, but I don’t see why that should not work as well.

Hope it helps.

Thaddy

  • Hero Member
  • *****
  • Posts: 15647
  • Censorship about opinions does not belong here.
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #10 on: August 10, 2024, 07:16:04 pm »
That is exactly what I already wrote, but he lost the plot.
If I smell bad code it usually is bad code and that includes my own code.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1408
    • Lebeau Software
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #11 on: August 10, 2024, 07:41:41 pm »
I find it interesting how the C++ language decided to implement inheritance like that of Class in Object Pascal, instead of using the multiple inheritance model.

What do you mean?  C++ supports multiple inheritance of classes and structs.  In fact, structs and classes are pretty much identical in all aspects of the language, except for one difference - struct members are public by default, whereas class members are private by default.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 15647
  • Censorship about opinions does not belong here.
Re: C++ Structs with inheritance, trying to sub it with something else.
« Reply #12 on: August 10, 2024, 07:57:09 pm »
Thanks Remy, you expressed it better than me.
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018