C++ OOP problem

Programming, for all ages and all languages.
Post Reply
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

C++ OOP problem

Post by PeterX »

When you develop an object file (.o) as part of a big application, there is one bad thing with C++:
You must put much of the code into header files. This thwarts the OOP principles like encapsulation/hiding.

Do you know about any OOP language who doesn't do that?

Greetings
Peter
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: C++ OOP problem

Post by iansjack »

I'm not clear what code needs to go in header files. Could you elucidate?
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: C++ OOP problem

Post by PeterX »

iansjack wrote:I'm not clear what code needs to go in header files. Could you elucidate?
I mean the private parts of a class (I count variable definitions as code here.) which have to go into a header file.

Furthermore I quote http://yosefk.com/c++fqa/defective.html
"In naturally written C++ code, changing the private members of a class requires recompilation of the code using the class."

I think there must be better OOP solutions. "Better" here means not having the above problems.

Greetings
Peter
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: C++ OOP problem

Post by iansjack »

Ah. I see what you're on about. I wouldn't define variable declarations as "code", but I see what you mean about encapsulation.

Surely the Pimpl Idiom deals with this problem?
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: C++ OOP problem

Post by klange »

PeterX wrote:Do you know about any OOP language who doesn't do that?
Languages like Java and C# solved this problem by having a bytecode format that includes all of the necessary metadata about classes.

Languages like Python solve this problem by representing nearly everything with a flexible map datastructure - you don't need to know what a class or even a superclass looks like at a compile time (and Python does compile, so it has a compile time) if you rely on duck typing and your methods and members are identified by strings at runtime.

C++ suffers from being purely textual in its compilation. The compiler needs to know what functions and members exist in a class so it can call the right offset into a vtable or whatever, but there's no encapsulated module format that can hide away the code and provide the compiler only a parseable machine representation of that information.
User avatar
eekee
Member
Member
Posts: 872
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

Re: C++ OOP problem

Post by eekee »

Not sure if you're looking for an alternative language, but I just saw mention of some in chat, & thought I'd pass it on.
incidentally yesterday i've stumbuled upon and took a brief look at some of these newfangled "c replacement" languages: zig, odin, v. i've also learned about nim, but it is my understanding nim's not so newfangled albeit looks quite nice.
there's a notable hole between c and something like rust, that's where these fit in
Nim looks the most mature and (I guess) suitable. "Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula." https://nim-lang.org/

The other 3 highlight their simplicity above all else. I don't know if that's what you'd want if you started with C++. :) Links because I have them handy: https://vlang.io/ http://odin-lang.org/ https://ziglang.org/
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: C++ OOP problem

Post by PeterX »

eekee wrote:Not sure if you're looking for an alternative language, but I just saw mention of some in chat, & thought I'd pass it on.
incidentally yesterday i've stumbuled upon and took a brief look at some of these newfangled "c replacement" languages: zig, odin, v. i've also learned about nim, but it is my understanding nim's not so newfangled albeit looks quite nice.
there's a notable hole between c and something like rust, that's where these fit in
Nim looks the most mature and (I guess) suitable. "Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula." https://nim-lang.org/

The other 3 highlight their simplicity above all else. I don't know if that's what you'd want if you started with C++. :) Links because I have them handy: https://vlang.io/ http://odin-lang.org/ https://ziglang.org/
These are some interesting suggestions.

1.) I already checked and excluded for various reasons: Forth, Rust, Go, D, normal Oberon, V.

2.) I also think that these are powerful but are too unpopular: Ada2012, ActiveOberon, Simula.
And these have difficult syntax for many programmers: Scheme, Smalltalk.
EDIT: I have to explain this: I want to make a open source community-based project.

3.) I will check the three [four] suggestions and Java.
The two languages that stick, are: C# and Python. As mentioned by klange.

Thanks to eekee and klange.

Happy hacking
Peter
User avatar
eekee
Member
Member
Posts: 872
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

Re: C++ OOP problem

Post by eekee »

Oh yeah, the pursuit of popularity is constraining. I almost cited 9front as an example of a thoroughly non-standard system which has nonetheless gained a small crowd, but then I remembered its status as a fork of Thompson and Ritchie's work gives it a big advantage. Despite that advantage, it's only gained a small crowd so far. Attempts to keep "Labs Plan 9"* alive and raise the popularity of Inferno* are not doing much better.

*: Thomson and Ritchie's original non-Unix OSs. 9front is forked from Plan 9.

You're welcome!
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C++ OOP problem

Post by Solar »

Would you elaborate on why the Pimpl idiom doesn't solve your "problem", or what your "problem" actually is?

Because most of the "solutions" mentioned here have their own set of "problems" with encapsulation, just a different kind of them...
Every good solution is obvious once you've found it.
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: C++ OOP problem

Post by PeterX »

Solar wrote:Would you elaborate on why the Pimpl idiom doesn't solve your "problem", or what your "problem" actually is?

Because most of the "solutions" mentioned here have their own set of "problems" with encapsulation, just a different kind of them...
Yes, there are problems with the solutions, too. That's normal.

I am still investigating pimpl. I'm using Python right now. I must emphasize that this is not for system programming.(I think in "General programming" forum it's ok to post about non-system-programming?)

Thanks to everyone.
Peter
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C++ OOP problem

Post by Solar »

PeterX wrote:I am still investigating pimpl.
CppReference.com's piece on the pattern.
Every good solution is obvious once you've found it.
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: C++ OOP problem

Post by PeterX »

Solar wrote:
PeterX wrote:I am still investigating pimpl.
CppReference.com's piece on the pattern.
I quote from that page:
pImpl breaks this compilation dependency; changes to the implementation do not cause recompilation. Consequently, if a library uses pImpl in its ABI, newer versions of the library may change the implementation while remaining ABI-compatible with older versions.
That's indeed a solution to my problem. Thanks.

Greetings
Peter
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C++ OOP problem

Post by Solar »

Note that C++ does not specify a stable ABI in the first place; ABI compatibility is implementation-defined. This means that changes in compiler, yes even a different compiler version, can make your libraries incompatible.

This is most pronounced when looking at Windows: A C++ library compiled with the MinGW compiler won't work with VisualC, and vice versa. I remember this being quite a hassle during GCC's 3.x version run as well, which brought numerous ABI changes.

Imagine what happens when a library offers a function call taking a std::string argument. The implementation of std::string that the library was "seeing" during compilation might be different from the one the client is "seeing" during compilation...

That is why many libraries written in C++ internally provide a C interface externally. Utility libraries, like Boost, being the obvious exception.
Every good solution is obvious once you've found it.
Post Reply