Recent

Author Topic: How C++ class multiple inherit and namespace to ObjFPC?  (Read 3635 times)

TYDQ

  • Full Member
  • ***
  • Posts: 171
How C++ class multiple inherit and namespace to ObjFPC?
« on: January 22, 2025, 03:23:05 am »
The sample C++ code is:
Code: C  [Select][+][-]
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Furniture
  9. {
  10. public:
  11.     Furniture(void) : m_weight(0){}
  12.     Furniture(double weight) : m_weight(weight){}
  13.     ~Furniture(void){}
  14.  
  15.     double GetWeight() const { return m_weight; }
  16.     void SetWeight(double val) { m_weight = val; }
  17.  
  18. private:
  19.     double m_weight;
  20.  
  21. };
  22.  
  23. class Bed : public Furniture
  24. {
  25. public:
  26.     Bed() : Furniture(), m_second(0) {}
  27.     Bed(double weight, int second) : Furniture(weight), m_second(second){}
  28.  
  29.     void Sleep(int second)
  30.     {
  31.         m_second = second;
  32.     }
  33.  
  34.  
  35. private:
  36.     int m_second;
  37.  
  38. };
  39.  
  40. class Sofa : public Furniture
  41. {
  42. public:
  43.     Sofa() : Furniture() {}
  44.     Sofa(double weight) : Furniture(weight){}
  45.  
  46.     void WatchTV(string  programme)
  47.     {
  48.     }
  49. };
  50.  
  51. class SleepSofa : public Bed, public Sofa
  52. {
  53. public:
  54.     SleepSofa() : Bed(), Sofa() {}
  55.     SleepSofa(double weight, int second) : Bed(weight, second), Sofa(weight) {}
  56.  
  57.     void FoldOut()
  58.     {
  59.         Sleep(360);
  60.     }
  61. };
How to translate these C++ code to Object Free Pascal?(The namespace can be self-defined,though.)

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #1 on: January 22, 2025, 09:07:49 am »
Object Pascal does not support multiple inheritance.
(even Bjarne Stroustrup, the creator of C++, thought that was a really bad idea with hindsight)
BUT...You can mimic it with interfaces.

Also, It is very rare in C++ circles that it is used, let alone used correctly.
It is the single most unloved "feature" of C++.

Using interfaces - in this case COM or CORBA does not matter - leads to easier to understand and maintain code. (Bjarne said so)

I wasted half of my life writing C and C++ code and only 25% writing Pascal and never needed that.
https://stroustrup.com/bs_faq2.html
https://www.usenix.org/legacy/publications/compsystems/1989/fall_stroustrup.pdf

So idiots that claim to the contrary can shut up immediately.
 >:( >:D O:-)

If Bjarne says no, and you still want to use it, you are an idiot.
Note I documented it undeniably so don't even bother to reason otherwise.
« Last Edit: January 22, 2025, 09:51:47 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

TYDQ

  • Full Member
  • ***
  • Posts: 171
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #2 on: January 22, 2025, 10:30:31 am »
Object Pascal does not support multiple inheritance.
(even Bjarne Stroustrup, the creator of C++, thought that was a really bad idea with hindsight)
BUT...You can mimic it with interfaces.

Also, It is very rare in C++ circles that it is used, let alone used correctly.
It is the single most unloved "feature" of C++.

Using interfaces - in this case COM or CORBA does not matter - leads to easier to understand and maintain code. (Bjarne said so)

I wasted half of my life writing C and C++ code and only 25% writing Pascal and never needed that.
https://stroustrup.com/bs_faq2.html
https://www.usenix.org/legacy/publications/compsystems/1989/fall_stroustrup.pdf

So idiots that claim to the contrary can shut up immediately.
 >:( >:D O:-)

If Bjarne says no, and you still want to use it, you are an idiot.
Note I documented it undeniably so don't even bother to reason otherwise.
OK,C++ is more complex than I can understand.Thanks to your advice.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #3 on: January 22, 2025, 10:54:43 am »
C++ is not complex, but multiple inheritance is - or can be - complex. And stupid.
( like with in Pascal)
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

alpine

  • Hero Member
  • *****
  • Posts: 1412
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #4 on: January 22, 2025, 11:27:04 am »
So idiots that claim to the contrary can shut up immediately.
 >:( >:D O:-)

If Bjarne says no, and you still want to use it, you are an idiot.
Note I documented it undeniably so don't even bother to reason otherwise.
I have used it sometimes by inheriting (multiple) pure virtual classes in purpose to mimic the D/FPC interfaces.  :-[
That way they can be useful, I think.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #5 on: January 22, 2025, 03:20:09 pm »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

alpine

  • Hero Member
  • *****
  • Posts: 1412
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #6 on: January 22, 2025, 04:36:01 pm »
I think.
Are you sure? :-X
Quite sure. Pure virtual classes inherited as interfaces.
Quote
Using interfaces - in this case COM or CORBA does not matter - leads to easier to understand and maintain code. (Bjarne said so)
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #7 on: January 22, 2025, 06:26:46 pm »
I told you so:
Quote
]BUT...You can mimic it with interfaces.
Good luck.
« Last Edit: January 22, 2025, 06:31:31 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

alpine

  • Hero Member
  • *****
  • Posts: 1412
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #8 on: January 22, 2025, 06:50:29 pm »
I told you so:
Quote
]BUT...You can mimic it with interfaces.
Good luck.
No, it is the other way around - using C++ MI with pure virtual classes in place of FPC interfaces. --Bjarne said so.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #9 on: January 22, 2025, 07:55:26 pm »
NO
Good luck,

If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

PascalDragon

  • Hero Member
  • *****
  • Posts: 6325
  • Compiler Developer
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #10 on: January 23, 2025, 10:12:52 pm »
NO
Good luck,

Yes, it is. C++ classes that only have virtual, abstract methods are essentially equivalent to CORBA interfaces (minus the ID string). Also C++ uses classes with virtual, abstract methods to represent COM interfaces (inheriting from a IUnknown base class).
C++ itself doesn't have interfaces as such.

VisualLab

  • Hero Member
  • *****
  • Posts: 714
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #11 on: January 24, 2025, 12:24:05 am »
Also, It is very rare in C++ circles that it is used, let alone used correctly.
It is the single most unloved "feature" of C++.

But not as rare as you suggest. This feature is used in the BeOS and Haiku OS APIs. Besides, these OSs are written in C++ (apart from parts of the kernel, which were written in C). And while I don't like C++ (and STL has particularly ugly code), the BeOS API was quite nice (for C++).

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #12 on: January 24, 2025, 01:39:03 pm »
Well, when I was learning for my Borland trainer certificate we struggled to come up with a single example.
« Last Edit: January 24, 2025, 08:19:26 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

TYDQ

  • Full Member
  • ***
  • Posts: 171
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #13 on: January 26, 2025, 04:08:05 pm »
C++ is not complex, but multiple inheritance is - or can be - complex. And stupid.
( like with in Pascal)
However,C++ grow more complex as many features announced in C++23 and later standard published.I think long history open-source C/C++ projects like binutils will use C++11 standard as the main standard to write the code.
So in your view what the C++ standard newest version of open-source C/C++ projects like binutils will use(I am writing my C2pas,so I want to know what feature in C++ will be used in these projects)?

PascalDragon

  • Hero Member
  • *****
  • Posts: 6325
  • Compiler Developer
Re: How C++ class multiple inherit and namespace to ObjFPC?
« Reply #14 on: January 26, 2025, 05:10:43 pm »
I think long history open-source C/C++ projects like binutils will use C++11 standard as the main standard to write the code.

GNU Binutils is written in C, not C++ and they won't simply change the language, so if at most they'll change to newer versions of the C standard. But even then the developers will very likely restrain themselves, because GNU Binutils are also available for older systems that don't support newer C standards.

 

TinyPortal © 2005-2018