|
Rage wrote: Why the question ? Because I have never seen good examples for public and private derivation.
|
|
|
|
|
Oh. I was hoping for a more crunchy story with secret services and hacking passwords.
|
|
|
|
|
Sorry to disappoint you, but I am a mere mortal programmer. I don't have time to brute force passwords and such...
|
|
|
|
|
That's because you do not use private and protected inheritance...
|
|
|
|
|
|
I have used private inheritance. The base class public access members are still public access in the derived class. Therefore the derived class still can call the base class functions and access its public data. But the user who instantiates the derived class, cannot access the public member of the base class, hence private inheritance.
Public inheritance is a "is-a" relationship.
Private inheritance is a "implemented-in-terms-of" relationship.
A useful example, is I like .NET string class and like a C++ string class with the same C# methods but I do not want to reimplement from scratch, so I derived from std::wstring with private inheritance to make use of its functionality, so that user of my string class cannot access the base class's std::wstring to avoid the confusion.
class MyString : private std::wstring
{
};
There is an excellent blog about this topic: C++ Tutorial: Private Inheritance - 2020
|
|
|
|
|
|
Hi David,
According to this, private inheritance in C++17 is primarily used whenever you're having a need to make all those public and protected members, in a base class, to become private in all derived descendant classes, and, thus not accessible via a derived class object.
Whist, according to this, all public and protected members of a base class become protected in all derived classes. This is typically needed to make those public members of a base class to become protected in all descendent classes, rather than those methods are accessible via a derived class object.
|
|
|
|
|
One instance where I used private inheritance in pre C++ 11 days was to easily make a class non-copyable:
class fancy_type : private boost::noncopyable
{};
|
|
|
|
|
At the end of the day, it’s a way to implement your public interface to the world without your clients being exposed to the details. You have many options to implement that interface; code directly in the class, delegating to others (composition) or inheriting it.
Imagine parallel universes, there is the public one that clients exist in and another that the non-public stuff lives in. Both (potentially) have a need for inheritance either to represent a natural taxonomy (CheckingAccount isa Account, etc) or for reuse (Account inherits Persistent, Equatable). I say “potentially” because out of all the ways to implement your public interface, inheritance is by far the worst in my opinion - it’s the road to spaghetti code .
|
|
|
|
|
Basically, public inheritance is an "IS-A" relationship while protected or private inheritance is a "HAS-A" relationship. A "HAS-A" relationship is like aggregation.
|
|
|
|
|
The only time I've used it is when deriving from a class to make use of its functionality, but not wanting to make all of the public base class members accessible. For example, making a fixed vector class that you can iterate over:
#include <iostream>
#include <vector>
template <class T> struct fixed_vector : private std::vector<T> {
using std::vector<T>::vector;
using std::vector<T>::begin;
using std::vector<T>::end;
};
int main() {
auto f = fixed_vector<int>{1,2, 3,4 ,5};
for(const auto i : f) {
std::cout << i << std::endl;
}
f.push_back(10); }
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
David O'Neil wrote: have you ever used protected and private inheritance?
All the time. Public is for the public parts of the interface to the class. The others are for the internal implementation details -- the parts we want to be free to rewrite and refactor without consequence. In my experience, a bigger public interface to a class means tighter coupling over time since other developers don't have to think very carefully about how their usage tightens the coupling, so they don't and just go ahead and use it.
5G -- more lies faster.
|
|
|
|
|
That is a great response, that makes me see things in a new light! Thanks! It is especially helpful breaking free from the 'aggregation/inheritance' argument, that never really made any sense to me since private inheritance is NOT aggregation!
|
|
|
|
|
Generally speaking, they are part of the encapsulation of function of your class - gating the interface is what I call it.
With that said, I live in the embedded world where so little code re-use occurs (other than cut/paste), OOD is routinely ignored.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
|
"The best of life is the simple things..."
|
|
|
|
|
Dim light bulbs or bright light bulbs - Watts the difference?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
That one's so bad it's shocking! I just may end up needing an IV to get through this.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
There are some witty answers lumen in the future. Fluorescent, or better, two, I'd say you have gotten your two cents worth in.
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
Management vs. Engineering?
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
OriginalGriff wrote: Dim light bulbs or bright light bulbs - Watts the difference?
Neh! It's cost.
|
|
|
|
|
Watt LED you to post this?
If you can't laugh at yourself - ask me and I will do it for you.
|
|
|
|
|
Wire you asking?
This question induces resistance in me.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Ohm, maybe you should conduct yourself a little better...
Anything that is unrelated to elephants is irrelephant Anonymous
- The problem with quotes on the internet is that you can never tell if they're genuine Winston Churchill, 1944
- Never argue with a fool. Onlookers may not be able to tell the difference. Mark Twain
|
|
|
|