overriding

Is there any way in C# to override a class method with an extension method?

孤街浪徒 提交于 2019-11-26 18:57:09
There have been occasions where I would want to override a method in a class with an extension method. Is there any way to do that in C#? For example: public static class StringExtension { public static int GetHashCode(this string inStr) { return MyHash(inStr); } } A case where I've wanted to do this is to be able to store a hash of a string into a database and have that same value be used by all the classes that use the string class's hash (i.e. Dictionary, etc.) Since the built-in .Net hashing algorithm is not guaranteed to be compatible from one version of the Framework to the next, I want

Why can't I access C# protected members except like this?

倖福魔咒の 提交于 2019-11-26 18:52:49
This code: abstract class C { protected abstract void F(D d); } class D : C { protected override void F(D d) { } void G(C c) { c.F(this); } } Generates this error: Cannot access protected member 'C.F(D)' via a qualifier of type 'C'; the qualifier must be of type 'D' (or derived from it) What in the world were they thinking? (Would altering that rule break something?) And is there a way around that aside from making F public? Edit: I now get the reason for why this is (Thanks Greg ) but I'm still a bit perplexed as to the rational; given: class E : C { protected override void F(D d) { } } Why

Override and overload in C++

徘徊边缘 提交于 2019-11-26 18:45:59
问题 Yes, I do understand the difference between them. What I want to know is: why OVERRIDE a method? What is the good in doing it? In case of overload: the only advantage is you haven't to think in different names to functions? 回答1: Overloading generally means that you have two or more functions in the same scope having the same name. The function that better matches the arguments when a call is made wins and is called. Important to note, as opposed to calling a virtual function, is that the

Overriding public virtual functions with private functions in C++

谁说胖子不能爱 提交于 2019-11-26 18:39:01
Is there is any reason to make the permissions on an overridden C++ virtual function different from the base class? Is there any danger in doing so? For example: class base { public: virtual int foo(double) = 0; } class child : public base { private: virtual int foo(double); } The C++ faq says that it is a bad idea, but doesn't say why. I have seen this idiom in some code and I believe that the author was attempting to make the class final, based on an assumption that it is not possible to override a private member function. However, This article shows an example of overriding private

Overriding a Base's Overloaded Function in C++ [duplicate]

陌路散爱 提交于 2019-11-26 18:33:49
Possible Duplicate: C++ overload resolution I ran into a problem where after my class overrode a function of its base class, all of the overloaded versions of the functions were then hidden. Is this by design or am I just doing something wrong? Ex. class foo { public: foo(void); ~foo(void); virtual void a(int); virtual void a(double); }; class bar : public foo { public: bar(void); ~bar(void); void a(int); }; the following would then give a compile error saying there is no a(double) function in bar. main() { double i = 0.0; bar b; b.a(i); } In class bar, add using foo::a; This is a common

Java: How to find if a method is overridden from base class? [duplicate]

江枫思渺然 提交于 2019-11-26 18:30:34
问题 This question already has answers here : How to quickly determine if a method is overridden in Java (8 answers) Closed 2 years ago . How to find out if a method is overridden by child classes? For example, public class Test { static public class B { public String m() {return "From B";}; } static public class B1 extends B { } static public class B2 extends B { public String m() {return "from B2";}; } /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) {

Can I get polymorphic behavior without using virtual functions?

∥☆過路亽.° 提交于 2019-11-26 18:25:33
问题 Because of my device I can't use virtual functions. Suppose I have: class Base { void doSomething() { } }; class Derived : public Base { void doSomething() { } }; // in any place { Base *obj = new Derived; obj->doSomething(); } the obj->doSomething() will call just the Base::doSomething() Is there a way with Base *obj , to call the doSomething of the Derived ? I know I can just put a virtual before doSomething() of Base it solve the problem, but I'm limited by my device, the compiler doesn't

Why is there no parameter contra-variance for overriding?

若如初见. 提交于 2019-11-26 18:08:57
问题 C++ and Java support return-type covariance when overriding methods. Neither, however, support contra-variance in parameter types - instead, it translates to over loading (Java) or hiding (C++). Why is that ? It seems to me that there is no harm in allowing that. I can find one reason for it in Java - since it has the "choose-the-most-specific-version" mechanism for overloading anyway - but can't think of any reason for C++. Example (Java): class A { public void f(String s) {...} } class B

Why does C#/CLR not support method override co/contra-variance?

给你一囗甜甜゛ 提交于 2019-11-26 17:51:53
问题 There are quite a few questions & answers about hacking around the limitation of C# not allowing method return (and argument) types to be changed to compatible types on overrides, but why does this limitation exist, either in the C# compiler or in the CLR? As I an see, there is nothing that could break if co/contra-variance was allowed, so what is the reasoning behind it? A similar question could be asked for widening access parameters - eg overriding a protected internal method with a public

Override valueof() and toString() in Java enum

旧时模样 提交于 2019-11-26 17:38:15
问题 The values in my enum are words that need to have spaces in them, but enums can't have spaces in their values so it's all bunched up. I want to override toString() to add these spaces where I tell it to. I also want the enum to provide the correct enum when I use valueOf() on the same string that I added the spaces to. For example: public enum RandomEnum { StartHere, StopHere } Call toString() on RandomEnum whose value is StartHere returns string "Start Here" . Call valueof() on that same