Type of base pointer



Take a look on this code:

using System;
using System.Collections.Generic;
using System.Text;

namespace BasePtr
{
class Program
{
class A
{
public virtual void Test()
{
Console.WriteLine("Method of base class");
}
}

class B:A
{
public override void Test()
{
Console.WriteLine("This is a real virtual method!");
}

public void OldTest()
{
base.Test();
}

public void ICanntUnderstand()
{
Console.WriteLine(base.GetType().ToString()); // WHY
IS THE OUTPUT "B?! base class for B is A not A"
}
}

static void Main(string[] args)
{
A a = new A(); // new A instance
a.Test(); // "Method of base class"
B b = new B(); // new B instance
b.Test(); // "This is a real virtual method!"
a = b;
a.Test(); // "This is a real virtual method!"
b.OldTest(); // "Method of base class"

b.ICanntUnderstand(); // WHY IS THE OUTPUT "B?! base class
for B is A not A"

Console.Read();
}
}
}

This code returns:

Method of base class
This is a real virtual method!
This is a real virtual method!
Method of base class
BasePtr.Program+B

I have a virtual function called Test() in class A. Also i have class B
- it's derived from A and has overrided method Test() as well.
I can easly access method A.Test() from class B - as written in
function B.OldTest().
As well base object doesn't have method OldTest(). You can see it in
this error description:

Error 1 'BasePtr.Program.A' does not contain a definition for
'OldTest' C:\Documents and Settings\Vitaliy\My Documents\Visual Studio
2005\Projects\BasePtr\BasePtr\Program.cs 27 22 BasePtr

base class for B is A and error above shows that base is
BasePtr.Program.A.
But code "base.GetType()" in any method of class B returns value
BasePtr.Program.B.

WHY?!

.



Relevant Pages

  • Re: inhibit compiler warning C4624 for a class hierarchy
    ... I'd really like to just pretend that the dynamic type is the base class. ... ** Carries a request or notification and any associated parameters. ... static void* operator new ... struct PNPEXPORT IConcurrentOperations::OpNotification abstract: public ...
    (microsoft.public.vc.language)
  • Re: Hooking automation object Events
    ... The next few lines of my post show the procedure SelectionChange: ... _PowerPCBDocEvents = dispinterface; ... void PowerPCBSink::OnFinalRelease ... // object before calling the base class. ...
    (comp.lang.pascal.delphi.misc)
  • RE: Protected keyword
    ... Only the Employee class internally can see the base class RaiseEvent method. ... public void HoldBreath{ ... a child that inherits from it so as to be able to call the protected method ...
    (microsoft.public.dotnet.languages.csharp)
  • Threads
    ... base class and then at least 2 classes that extends Thread. ... void upDateScreen() ...
    (comp.lang.java.programmer)
  • Re: memory usage of Virtual function poiters
    ... >void foo ... have an extra hidden data pointer (the pointer to the virtual function ... member which begs the question as to why you had 20 originally. ... but also how could this design meet the same needs as the previous ...
    (alt.comp.lang.learn.c-cpp)