Lazarus

Free Pascal => Beginners => Topic started by: TYDQ on January 22, 2025, 03:23:05 am

Title: How C++ class multiple inherit and namespace to ObjFPC?
Post by: TYDQ 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.)
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: Thaddy 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.
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: TYDQ 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.
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: Thaddy 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)
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: alpine 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.
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: Thaddy on January 22, 2025, 03:20:09 pm
I think.
Are you sure? :-X
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: alpine 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)
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: Thaddy on January 22, 2025, 06:26:46 pm
I told you so:
Quote
]BUT...You can mimic it with interfaces.
Good luck.
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: alpine 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.
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: Thaddy on January 22, 2025, 07:55:26 pm
NO
Good luck,

Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: PascalDragon 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.
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: VisualLab 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++).
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: Thaddy 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.
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: TYDQ 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)?
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: PascalDragon 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.
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: Thaddy on January 26, 2025, 05:20:12 pm
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)?
It is not multiple inheritance (which is C++ only and not C) The C standards can't be flawed for that.
Multiple inheritance is also not new but part of the very first C++ standards.

The problem it has is scoping. It will bite you eventually.

With the C standards I have basically no problems at all in using newer versions.
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: TYDQ on January 27, 2025, 08:49:05 am
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.
C++ exists in the newer version of binutils.gold(elf-only linker) is developed using C++,we cannot guarantee whether C++ developers join in GNU organization.
Looking into binutils you will find some C++ Source code files(like binutils-2.43/gold/archive.cc).
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: Thaddy on January 27, 2025, 11:04:36 am
So you are not referring to the compiler, but that the gold linker has C++ support?
The old linker has also C++ support..... You mix things up.
Title: Re: How C++ class multiple inherit and namespace to ObjFPC?
Post by: TYDQ on January 28, 2025, 01:23:16 pm
So you are not referring to the compiler, but that the gold linker has C++ support?
The old linker has also C++ support..... You mix things up.
Yes,my developing c2pas is specialized to C projects such as binutils.
C++ code is out of my c2pas goal,but binutils source code have appeared many C++ codes,so I took them into consideration.
TinyPortal © 2005-2018