-1

I tried the following example in C++. I understood that redefining Method4() from CA class, with Method4(int a) in CB, hides Method4() only from the visibility point of view, not from VMT perspective. Therefor, the first 2 calls in Main function are not working.

class CA
{
public:
    virtual void Method1() { }
    virtual void Method2() { }
    virtual void Method3() { }
    virtual void Method4() { }
};

class CB : public CA
{
public:
    void Method1() { }
    void Method2() { }
    void Method4(int a) { }
};

int main()
{
    CB b1;
    b1.Method4();   // IT'S NOT WORKING (I EXPECTED)
    
    CB* b2 = new CB;
    b2->Method4();  // IT'S NOT WORKING (I EXPECTED)
    
    CA* a = new CB; 
    a->Method4();   // IT'S WORKING (I EXPECTED) 

    return 0;
}

I tried to reproduce the example in C#. To my surprise, the following call is perfectly ok.

CB b1 = new CB(); // I EXPECTED NOT TO WORK, BUT IT DOES WORK
b1.Method4();

My question is, why ? Is this a real difference between the two languages or am I missing something ?

Following your fair suggestions, I have added the C# code below:

public class CA
{
    public virtual void Method1() { }
    public virtual void Method2() { }
    public virtual void Method3() { }
    public virtual void Method4() { }
}

public class CB : CA
{
    public override void Method1() { }
    public override void Method2() { }
    public void Method4(int a) { }
}

public static void Main()
{
    CB b = new CB(); // IT'S WORKING (I DIDN'T EXPECT THIS)
    b.Method4();
}
  • In C#, instances of classes are reference types, which are similiar to pointers in C++. The C# code is more similiar to the second call in your C++ code. So yes, there is a real difference in the two languages. In my opinion, C# is more close to Java than to C++. – SomeBody Mar 21 '23 at 10:24
  • I'm no C# guru (or even close to one) but I think we would need to see some *actual* C# code (equivalent to your C++ version) to be able to give any meaningful diagnosis. – Adrian Mole Mar 21 '23 at 10:45
  • @SomeBody Yes, a fair point that addresses the difference between the C# code and the ***first*** C++ attempted call. But the second error in the OP's C++ code uses a pointer, but is still an error. I *know* why that C++ code doesn't work but we need to see a fuller C# snippet (IMHO) in order to give a proper answer. – Adrian Mole Mar 21 '23 at 10:47
  • @SomeBody, you are right. I know what you mean. That first call is not very relevant for this issue. – oopEnthusiast Mar 21 '23 at 11:01
  • Because in C++, functions with the same name will hide all the functions in the base class, there's no special reasons, the rule may have been set since the birth of C++, it also applies to non-virtual functions. – shingo Mar 21 '23 at 11:15
  • What ist the "VMT perspective"? – 463035818_is_not_an_ai Mar 21 '23 at 16:21

1 Answers1

2

I understood that redefining Method4() from CA class, with Method4(int a) in CB, hides Method4()

Not in C#. Method4() and Method4(int a) have different signatures, so for the purpose of inheritance they are like different methods.

There is a concept of hiding in c#, but it is different:

class CA{
    public void Method(){Console.WriteLine("CA");}
}
class CB : CA{
    public new void Method(){Console.WriteLine("CB");}
}

In this case the called method will depend on the type of the reference. So CB.Method will "hide" CA.Method. This is made explicit by the new keyword, omitting this will generate a warning.

JonasH
  • 28,608
  • 2
  • 10
  • 23