user562566
user562566

Reputation:

Possible to remove inherited members or prevent inheritance of certain properties/methods?

I'm wondering if it is at all possible to "remove" members/methods from a base class, or rather inhibit them for being inherited?

Say I have a class A that defines a subscript operator and stores the read/writes of that operator in a map object internally. Then I create B which inherits from A. Is there any way I can remove the subscript operator from B, or rather, prevent it from being inherited and thus becoming part of B?

Sorry for being so verbose, but to make it clear, I'm not looking for functionality such as the final keyword, where I want to prevent B from overloading something in A. I'm looking to basically say "B, inherit everything from A EXCEPT X". I figure it's not possible and most likely the answer is going to be "fix/improve your design", just thought there was no harm in asking anyway.

Upvotes: 2

Views: 2300

Answers (1)

vsoftco
vsoftco

Reputation: 56567

Make the function (or the member) that you want to "delete" private, so the derived class will not be able to "see it". There is no other way of physically removing the function from the derived object (as the derived object contains a copy of the base class, in which your function/member is needed), but making it private guarantees that the derived object has no access to it.

Upvotes: 3

Related Questions