Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does private mean different things in C++ and C#?

I was wondering why C# doesn't allow private virtual functions and ran across the aptly named Why are private virtual methods illegal in C#?

In the accepted answer Eric Lippert (who probably knows what he's talking about...) said:

If you desire to restrict the ability to override the method in non-nested derived classes then you can do so by restricting the ability of non-nested classes to derive from the base class;

In C++ private: virtual makes sense because it means "I want classes derived from me to override the functionality of this function but they shouldn't be able to call it directly", in other words private controls who can call a function and has no effect on who can override it.

I realize that since only derived classes can override in the first place and since C# prohibits private virtual functions this question may be meaningless, are there other scenarios in which the protection level of a function can effect who can override it (protected internal perhaps)?

like image 221
Motti Avatar asked Nov 25 '10 12:11

Motti


1 Answers

It sounds like you want the C++ world of being able to override but not call; then no: you can't do that in C#. The other way around is fine, though - either don't mark it virtual in the first place, or mark it sealed if it is already virtual.

like image 173
Marc Gravell Avatar answered Sep 19 '22 03:09

Marc Gravell