Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get Base Class Instance from a Derived Class

I don't know if this is possible, but I am trying to get the Base Class instance from a Derived Class. In C#, I can use the base keyword to access properties and methods of the Base Class (of course), but I want to use base itself. Attempting to do so results in a "Use of keyword 'base' is not valid in this context" error.

Example Code

public class SuperParent
{
    public int SPID;

    public SuperParent()
    {
    }
}

public class SubChild : SuperParent
{
    public SubChild(int pSPID)
    {
        base.SPID = pSPID;
    }

    public int BaseSPID
    {
        get
        {
            SuperParent sp = base;
            return sp.SPID;
        }
    }
}
like image 334
OneSource Avatar asked Oct 08 '13 08:10

OneSource


People also ask

How do you access base class members in a derived class?

You can use both a structure and a class as base classes in the base list of a derived class declaration: If the derived class is declared with the keyword class , the default access specifier in its base list specifiers is private .

Can we create a base class object from derived class?

No, that's not possible since assigning it to a derived class reference would be like saying "Base class is a fully capable substitute for derived class, it can do everything the derived class can do", which is not true since derived classes in general offer more functionality than their base class (at least, that's ...

How is a base class inherited by a derived class?

The Base class members and member functions are inherited to Object of the derived class. A base class is also called parent class or superclass. Derived Class: A class that is created from an existing class. The derived class inherits all members and member functions of a base class.

Which class is an instance of base class?

Instances of the derived class are instances of the base class.


1 Answers

If you're working with an instance of the derived class, there is no base instance. An example:

class A
{
    public void Foo() { ... }
}

class B : A
{
    public void Bar() { ... }
}

What is not possible within B:

public void Bar()
{
    // Use of keyword base not valid in this context
    var baseOfThis = base; 
}

You can do something like this:

public void Bar()
{
    base.Foo();
}

And you can add another method like

public A GetBase()
{
    return (A)this;
}

And then you can

public void Bar()
{ 
    var baseOfThis = GetBase();
    // equal to:
    baseOfThis = (A)this;
}

So this GetBase() method is probably what you want.

The punchline is: If you have an instance of B, it inherits all properties and the non-overriden behaviour of A, but it does not consist of an instance of B which holds an (hidden but automatic) reference to an instance of A. You can cast your B instance to A, but it remains to be an instance of B.

like image 184
Matten Avatar answered Oct 14 '22 08:10

Matten