473,770 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic method dispatching

Hi,

could someone please exmaplin to me what dynamic method dispatching is
and how it's connected to virtual methods? Thanks

Terry

Jul 19 '05 #1
1 2502

"Terry" <te***@nospam.c om> wrote in message
news:3f******** @clarion.carno. net.au...
Hi,

could someone please exmaplin to me what dynamic method dispatching is
and how it's connected to virtual methods? Thanks

Terry


Virtual methods use dynamic dispatching. This means that the method that is
called depends on the type at run time, not the type at compile time.

E.g.

class B
{
public:
virtual void func() { cout << "its B" }
};

class D : public B
{
public:
virtual void func() { cout << "its D" }
};

B* b_ptr = new D();
b_ptr->func();

Which func is called, B::func or D::func? At compile time b_ptr is a B* so
you could argue that B::func is called (static dispatching). But when the
program runs b_ptr happens to be pointing to a D, so you could argue that
D::func is called (dynamic dispatching).

The answer in this case is the D::func is called. Because func is virtual
dynamic dispatching is used. If func where not virtual then B::func would be
called.

Why don't you try it and see? Then remove virtual and try again.

john
Jul 19 '05 #2

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

Similar topics

12
1907
by: Donnal Walter | last post by:
The following method is defined in one of my classes: def setup(self, items={}): """perform setup based on a dictionary of items""" if 'something' in items: value = items # now do something with value if 'another' in items: value = items # do something else with value
3
1312
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example below... --
0
1631
by: Edson Tadeu | last post by:
I was thinking in a way to do static dispatching on enumerations, in a way similar to dispatching on integral constants using Loki's Int2Type<> or Boost.MPL's int_<>, i.e, creating types based on the enumeration constants, so I came up with this example code: #include <iostream> using namespace std; template <class EnumType>
4
2205
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class that several classes inherit from. From time to time I need to add new inherited types so I want something that is as versatile as possible, preferably not requiring naming all subtypes in a switch statement.
5
12050
by: markww | last post by:
Hi, Can someone explain to me what static and dynamic dispatch are and what the difference is between the two? Do this even occurr in C++? Thanks
11
28148
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
2
1772
by: doomer | last post by:
Hello, I'm trying to build a set of classes that act as the handlers for a simple expression parser, and i'm wondering how to preserve the dynamic type information from derived classes. An example will help illustrate what I have in mind... class Numeric { // contains discriminated union to hold data and identify data
7
2909
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its method called and then the derived class have its method called. What am I not understanding? Int the following code, my Event Tester class is getting called twice for keyboard events when I step through the debugger:...
1
1253
by: shaileshkumar | last post by:
i have developed three servlets whose registered names are Demo,Next & Last respectively. //code for dispatching to Next in Demo RequestDispatcher rd=getRequestDispatcher("Next"); rd.forward(req,res); //code for dispatching to Last in Next RequestDispatcher rd1=getRequestDispatcher("Last"); rd.forward(req,res);
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.