473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Suppress virtual call mechanism


This must be fairly straightforward as I used to do this quite often
in C++ but I seem to be missing something really obvious. How do I
force the code to call Foo.DoThis() inside Foo.CallIntoDoThis(), even
though the current instance is of type Bar (and hence the call always
dispatches to Bar.DoThis())?

public abstract class Baz
{
public abstract void DoThis();
}

public class Foo : Baz
{
public override void DoThis()
{
Console.WriteLine("Foo.DoThis");
}

public void CallDoThis()
{
DoThis();
// how do I force a call to Foo.DoThis() here???
((Foo)this).DoThis(); // this doesn't work
}
}

public class Bar : Foo
{
public override void DoThis()
{
Console.WriteLine("Bar.DoThis");
}

public void CallIntoFoo()
{
CallDoThis();
}
}

class Program
{
static void Main(string[] args)
{
Bar b = new Bar();
b.CallIntoFoo();
}
}
Aug 22 '08 #1
3 1201
On Fri, 22 Aug 2008 10:23:16 -0700, Dilip <rd*****@lycos.comwrote:
This must be fairly straightforward as I used to do this quite often
in C++ but I seem to be missing something really obvious. How do I
force the code to call Foo.DoThis() inside Foo.CallIntoDoThis(), even
though the current instance is of type Bar (and hence the call always
dispatches to Bar.DoThis())?
The usual pattern would be to require the _override_ to manage it. That
is, in class Bar:

public override void DoThis()
{
Console.WriteLine("Bar.DoThis");
base.DoThis();
}

You could implement the literal behavior you're showing by making
Foo.DoThis() call a non-virtual helper method that the Foo class knows
about:

public override void DoThis()
{
_DoThisImpl();
}

public void CallDoThis()
{
DoThis();
// how do I force a call to Foo.DoThis() here???
_DoThisImpl();
}

private void _DoThisImpl()
{
Console.WriteLine("Foo.DoThis");
}

But I don't like that pattern much, at least not in this case, because you
could conceivably wind up calling the same method twice (if you have an
instance of Foo, not Bar). That seems likely to be wrong, but of course
only you would know for sure if that's really what you meant to do.

Pete
Aug 22 '08 #2
On Aug 22, 1:01*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 22 Aug 2008 10:23:16 -0700, Dilip <rdil...@lycos.comwrote:
This must be fairly straightforward as I used to do this quite often
in C++ but I seem to be missing something really obvious. *How do I
force the code to call Foo.DoThis() inside Foo.CallIntoDoThis(), even
though the current instance is of type Bar (and hence the call always
dispatches to Bar.DoThis())?

The usual pattern would be to require the _override_ to manage it. *That *
is, in class Bar:

* * *public override void DoThis()
* * *{
* * * * *Console.WriteLine("Bar.DoThis");
* * * * *base.DoThis();
* * *}

You could implement the literal behavior you're showing by making *
Foo.DoThis() call a non-virtual helper method that the Foo class knows *
about:

* * *public override void DoThis()
* * *{
* * * * *_DoThisImpl();
* * *}

* * *public void CallDoThis()
* * *{
* * * * *DoThis();
* * * * *// how do I force a call to Foo.DoThis() here???
* * * * *_DoThisImpl();
* * *}

* * *private void _DoThisImpl()
* * *{
* * * * *Console.WriteLine("Foo.DoThis");
* * *}

But I don't like that pattern much, at least not in this case, because you *
could conceivably wind up calling the same method twice (if you have an *
instance of Foo, not Bar). *That seems likely to be wrong, but of course *
only you would know for sure if that's really what you meant to do.

Pete
Pete

Thanks very much. I knew the usual base.DoThis() pattern but in my
case I wanted the literal behaviour as I laid it out in my post.
Thankfully your reply gave me an idea and I ended up implementing it
successfully.

Cheers!
Aug 22 '08 #3
- have Bar call its base implemenation if DoThis :

public class Bar : Foo
{
public override void DoThis()
{
base.DoThis();
Console.WriteLine("Bar.DoThis");
}

public void CallIntoFoo()
{
CallDoThis();
}
}
- or - don't depend on the derived class calling its base implementation and
use an private Foo method :

public class Foo : Baz
{
private void FooDoThis()
{
Console.WriteLine("Foo.FooDoThis");
}

public void CallDoThis()
{
DoThis();
FooDoThis();
}
}

"Dilip" <rd*****@lycos.comwrote in message
news:71**********************************@j22g2000 hsf.googlegroups.com...
>
This must be fairly straightforward as I used to do this quite often
in C++ but I seem to be missing something really obvious. How do I
force the code to call Foo.DoThis() inside Foo.CallIntoDoThis(), even
though the current instance is of type Bar (and hence the call always
dispatches to Bar.DoThis())?

public abstract class Baz
{
public abstract void DoThis();
}

public class Foo : Baz
{
public override void DoThis()
{
Console.WriteLine("Foo.DoThis");
}

public void CallDoThis()
{
DoThis();
// how do I force a call to Foo.DoThis() here???
((Foo)this).DoThis(); // this doesn't work
}
}

public class Bar : Foo
{
public override void DoThis()
{
Console.WriteLine("Bar.DoThis");
}

public void CallIntoFoo()
{
CallDoThis();
}
}

class Program
{
static void Main(string[] args)
{
Bar b = new Bar();
b.CallIntoFoo();
}
}

Aug 22 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Prabu | last post by:
Hi, I'm new to python, so excuse me if i'm asking something dumb. Does python provide a mechanism to implement virtual functions? Can you please give a code snippet also...:) Thanx in advance...
3
by: Sensorflo | last post by:
struct CBase { virtual inline void foo() { puts("base"); } }; void main() { CBase obj; (&obj)->foo(); // static call or } // dynamic (using virtual table) call ? Is it...
4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
4
by: mangesh | last post by:
virtual mechanism is activated on pointers only , not on objects . what is reason beind this . Regards Mangesh .
6
by: Gerhard Prilmeier | last post by:
Hello, I have an unmanaged C++ API that uses virtual functions, like this: class A { public: virtual void handleMe(){} };
3
by: xtrigger303 | last post by:
Hi to all, a newbie question. Is it possible to avoid the virtual mechanism with pointers to members syntax the same way it's done with an explicit call ( obj.Base::Func() )? It would be...
2
by: cpp | last post by:
I have a class X. Then class Y is inheriting from X. I have another class Base which is having a virtual function virtual void fun(X& x){ } Then i have class Derived which is inheriting form...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.