Skip to main content
added 367 characters in body
Source Link
occipita
  • 209
  • 2
  • 5

There are (or at least should be) no applications where this feature is actually useful. The reason for this is that the only thing it allows you to do is this:

class A : protected B {
   ...
   void something () { doSomethingThatRequiresB(this); }
];};

But this is exactly equivalent to:

class A {
protected:
   B myB;
   ...
   void something () { doSomethingThatRequiresB(&myB); }
];};

The latter, however, is easier to understand so is always preferable.

The only caveat is that, per the answer referenced in Doc Brown's comment to the question, it appears in some circumstances (particular if the base class is empty) the former can trigger some optimisations that the latter does not, but at least theoretically they should be the same, and I would consider the lack of optimisation in the latter case a shortcoming of the compiler, not a problem of the code.

There are no applications where this feature is actually useful. The reason for this is that the only thing it allows you to do is this:

class A : protected B {
   ...
   void something () { doSomethingThatRequiresB(this); }
];

But this is exactly equivalent to:

class A {
   B myB;
   void something () { doSomethingThatRequiresB(&myB); }
];

The latter, however, is easier to understand so is always preferable.

There are (or at least should be) no applications where this feature is actually useful. The reason for this is that the only thing it allows you to do is this:

class A : protected B {
   ...
   void something () { doSomethingThatRequiresB(this); }
};

But this is exactly equivalent to:

class A {
protected:
   B myB;
   ...
   void something () { doSomethingThatRequiresB(&myB); }
};

The latter, however, is easier to understand so is always preferable.

The only caveat is that, per the answer referenced in Doc Brown's comment to the question, it appears in some circumstances (particular if the base class is empty) the former can trigger some optimisations that the latter does not, but at least theoretically they should be the same, and I would consider the lack of optimisation in the latter case a shortcoming of the compiler, not a problem of the code.

Source Link
occipita
  • 209
  • 2
  • 5

There are no applications where this feature is actually useful. The reason for this is that the only thing it allows you to do is this:

class A : protected B {
   ...
   void something () { doSomethingThatRequiresB(this); }
];

But this is exactly equivalent to:

class A {
   B myB;
   void something () { doSomethingThatRequiresB(&myB); }
];

The latter, however, is easier to understand so is always preferable.