Calling a base class virtual function 
Author Message
 Calling a base class virtual function

Quote:

> D1 derives from D which derives from B and all of them
> implement a virtual function f().
> If I have an instance of D1 can I use it to call directly
> the f() fuction of B.
> In C++ one can code:
> D1 d1 = new D1();
> d1->B::f();
> In such a case the implementation of B::f() will be called.

> How and if is this possible in C#.

No, I don't believe that's possible. (Good job too, IMO - going behind a
derived class's back is a bad idea, as far as I can see.)

--

http://www.*-*-*.com/ ~skeet/
If replying to the group, please do not mail me too



Tue, 10 May 2005 23:37:24 GMT  
 Calling a base class virtual function

Adding a non virtual function to the base class is not always possible.
Some times we do not have the source code for the base class or we just
do not want to change it for different reasons.
It seems that in this area C# is much less flexible than C++.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Wed, 11 May 2005 00:49:20 GMT  
 Calling a base class virtual function
Well, yes although I'd qualify it as: "less flexible"=="enforces
encapsulation and class inheretance hierarchy".  I don't know the specifics
of your case, but with the limited information I see, the problem might be
solvable by design (another derivation of B, perhaps?).

Richard


Quote:

> Adding a non virtual function to the base class is not always possible.
> Some times we do not have the source code for the base class or we just
> do not want to change it for different reasons.
> It seems that in this area C# is much less flexible than C++.

> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Wed, 11 May 2005 01:00:55 GMT  
 Calling a base class virtual function
******************************
You can't get to the B base with this, but you can get to C from D with

class D : C
{
    public void fOfC()
    {
        base.f();    // Runs the C version.
    }

Quote:
}

******************************
class B
{
    public virtual void f()
    {
        // Runs the B version
    }
Quote:
}

class C : B
{
    public override void f()
    {
        // Runs the C version
    }
    public virtual void fOfB()
    {
        base.f();    // Runs the B version.
    }

Quote:
}

class D : C
{
    public override void f()
    {
        // Runs the D version
    }
    public virtual void fOfC()
    {
        base.f();    // Runs the C version.
    }
    // Bootstrapping through C.
    public override fOfB()
    {
        base.fOfB();    // Runs the B version.
    }
Quote:
}

******************************

Chris R.


Quote:
> Hi,
> D1 derives from D which derives from B and all of them
> implement a virtual function f().
> If I have an instance of D1 can I use it to call directly
> the f() fuction of B.
> In C++ one can code:
> D1 d1 = new D1();
> d1->B::f();
> In such a case the implementation of B::f() will be called.

> How and if is this possible in C#.

> Thanks



Wed, 11 May 2005 01:34:31 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. virtual function - calling derived version from base class

2. Call virtual function in base class

3. Calling a virtual function in base class

4. Inherited class function calls from a pointer assigned to the base class

5. Virtual function in base class constructor?

6. Problem with virtual functions in dll-based class?

7. Serialize class derived from virtual base class

8. Compiler error on call to base class function

9. Calling Functions of the Base Class?

10. Calling overloaded function in base from another overload in a derived class

11. Calling a base classes member function

12. How to make call to base class function == a compile error (restated)

 

 
Powered by phpBB® Forum Software