0

My assignment tells me to use both private and public parts from a parent class in a child class.

However doing something like

class CarInsurance: public Insurance, private Insurance
{
public:
//some stuff...
private:
//some more stuff...
}

is a duplicate of the same class, which is not possible.

Is there any other way to do this without having to make multiple child classes? I just want to have one child class, which can use both public and private parts from the parent class.

  • you seem to misunderstand the meaning of public vs private inheritance. Private inheritance does not grant you access to private fields in the base class. – 463035818_is_not_an_ai Oct 21 '19 at 11:22
  • 4
    Public and private inheritance is unrelated to "use both private and public parts from a parent class". For me the word "use" means to actually *using* members from the parent class. And you simply can't use private members of a base class. – Some programmer dude Oct 21 '19 at 11:22
  • 1
    You can't access private members of a parent class, even in public inheritance. You'd have to make the parent's fields protected instead. – nada Oct 21 '19 at 11:23
  • 2
    And if you're unsure about the assignment, then please ask your teacher or teachers assistant instead. – Some programmer dude Oct 21 '19 at 11:24
  • taking the assignment literally it either requires you to use some dirty hack (not recommended) or it has no solution. The child class cannot access private members of the base directly – 463035818_is_not_an_ai Oct 21 '19 at 11:26
  • Access checks are not made when writing template specialisations. That allows you to write a legitimate pattern to access private members, with some effort. – Bathsheba Oct 21 '19 at 11:28
  • @Bathsheba Is that a defect of the standard??? There was some question about somewhere here recently (breaking private inheritance with templates). – Aconcagua Oct 21 '19 at 11:36
  • @Aconcagua no it is not, I guess the cat refered to the [template backdoor](https://stackoverflow.com/a/424990/4117728). Somehow funny that some first consider a hack before `friend` – 463035818_is_not_an_ai Oct 21 '19 at 11:42
  • @formerlyknownas_463035818 Well, the assignment sounds as if the base class may be a given, which often will be the case in RL. In that case there are only hacks left. – Peter - Reinstate Monica Oct 21 '19 at 11:44
  • @formerlyknownas_463035818 No, wasn't that one. I remember a template inheriting from a normal class and with some tricks (specialisation? – don't know any more), the template could access private members. Not all compilers seemd to accept it, though, but answers/comments weren't specific about behaviour mandated by standard. – Aconcagua Oct 21 '19 at 11:47
  • @Aconcagua Didnt see that one. Anyhow, like many other things (most prominently `const`), `private` is to help the programmer. There are ways to work around them, and if you do you just get what you deserve. Just recently there was an interesting question about bypassing the `const`ness of a vectors elements ([here](https://stackoverflow.com/questions/58419749/how-to-sort-a-constant-vector-without-using-any-extra-memory)), in a wider sense it is the same story: you can break it, but you better dont – 463035818_is_not_an_ai Oct 21 '19 at 12:02
  • @formerlyknownas_463035818 *'ways to work around'* are means to break a contract deliberately. If those tools are broken, then it is another matter. That seemed to be the case in the aforementioned question, though. Need to find that one again... – Aconcagua Oct 21 '19 at 12:27

1 Answers1

2

Yes, you can do this by befriending classes together.

The notion is:

class Insurance
{
// among other things add this line (doesn't matter if its public, protected or private)
friend class CarInsurance;
}

//////////////////////////

class CarInsurance: public Insurance // we need public here
{
public:
//some stuff...
private:
//some more stuff...
}
PiotrK
  • 4,210
  • 6
  • 45
  • 65