virtual table; override; new, question

R

relient

I'm learning about the virtual table in association with virtual
methods. I got most of the logic and understanding down (I believe) for
when you use 'override' and no 'override' or no 'new' modifier. What
I'm trying to figure out now is what happens when you do use modifier
'new' on a derived class method inherited from the base class?


My understanding for the override mechanism with the following code:

class Base
{
public virtual void Method( )
{
}
}

class Derived : Base
{
public override void Method( )
{
}
}


...is that the vtable will replace the Base.Method() address by making
the slot in the array of pointers (where Base.Method resides) point to
the new override method declared in the Derived class. Correct?

My understanding for when you do not 'override' or 'new' the base
method in the derived class is this: The vtable will not change where
the Base.Method address resides in the the slot in the array of
pointers, of the vtable, so, we'll be using the same base method. Yes?

Now to my main question: what happens when you do use the 'new'
modifier on the derived method? How will the vtable look?

Thanks in advance.
 
M

Mattias Sjögren

My understanding for when you do not 'override' or 'new' the base
method in the derived class is this: The vtable will not change where
the Base.Method address resides in the the slot in the array of
pointers, of the vtable, so, we'll be using the same base method. Yes?

Right. The method in the derived class will get its own vtable slot.

Now to my main question: what happens when you do use the 'new'
modifier on the derived method? How will the vtable look?

The same thing as when you don't specify new or override. The only
difference is that you get a compiler warning if you don't specify
anything.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top