Type of base pointer

D

dotNiemand

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?!
 
P

Peter Duniho

dotNiemand said:
[...]
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?!

Because the type of the object is "B", not "A".

Your code says to call the "GetType()" method on the "base" class ("A", in
this case). You haven't overridden the "GetType()" method in your "A" class
(nor should you have), so you get the default behavior for "GetType()",
which is to return the true type of the instance. In the words of the
*documentation*: "The Type instance that represents the exact runtime type
of the current instance"

Since the instance was created as type "B", that's what "GetType()" returns.

Note that if you inherited your "B" class in a third class "C", and then
called your "ICanntUnderstand()" method on an instance of "C", it would
return "C", not "B". The result of that method depends not on the class in
which it's defined, but rather the type of the object on which it's called.

Depending on what you're trying to do exactly, you may find that the C#
"typeof" operator is actually what you want.

Pete
 

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