Static Binding & Dynamic Binding :: C++

Status
Not open for further replies.
Hi.

Is there an advantage to using dynamic binding instead of static binding?

I have both Deitel & Deitel C++ How to Program and Stroustrup's Special Edition. I read all of the first book and finished chapter fifteen of Stroustrup. So I understand virtual functions and virtual base. However, I am not to the point where I can implement virtual functions and virtual base without asking: How do you implement them effectively?

For example, "virtual" only works for pointer and reference. That is completely conceivable. However, it seems you have to declare an object of a derived class and then *cast* a new class from the base class to the derive class just to get it working right.

-----------------
classDerived cDerived = new classDerived;

classBase *cBase = &cDerived;
-----------------

The example above comes straight from Deitel & Deitel. So everything looks okay. What is the point of declaring two separate classes? I understand that the use of "virtual," but I do not understand the performance of "virtual" if you have to declare an object of the derived class and an object of the base class. Under what specific situation do you implement dynamic binding? Please be very specific because I found that neither Deitel & Deitel nor Stroustrup give a convincing example of dynamic binding.

Lastly, I too want to implement dynimic binding if it is possible to declare *one* object of either the base class or the derived class. The following is *not correct*:

------------------------
classBase *cDynamic = new classDerived
------------------------

In other words, *virtual* would be great if there is a way to use it without having to declare two seperate classes like the example from Deitel & Deitel.

Thanks,
Kuphryn
 

earl

Well-known member
3,066
Run this code and think about it.

File: test.h
code:<HR>
Code:
class base
{
public:
  virtual void vprint(void)=0;
};
  
class derived: public base
{
public:
   void vprint(void)
   {
      printf("in derived virtual\n");
   }
};
  
  
class normalBase
{
public:
   void print(void)
   {
      printf("normal: in base\n");
   }
};
  
class normalDerived: public normalBase
{
public:
   void print(void)
   {
      printf("normal: in derived\n");
   }
};

test.cpp:
code:<HR>
Code:
#include<stdio.h>
  
#include"test.h"
  
int main(int argc, char** argv);
  
int main(int argc, char** argv)
{
  
   /* examine what happens w/ virtual */
   derived D;
  
   D.vprint();
   base* b;
   b = &D;
   b->vprint();
  
   /* contrast with non-virtual inheritance */
   normalDerived ND;
   ND.print();
   normalBase* nb = & ND;
   nb->print();
  
  
   return 0;
}

-earl-

Edit: it's really annoying to have to go through and put spaces in to keep blank lines.
 
Thanks earl.

The second part of the code does not work. The program does not know which print() you are talking about.

I understand the use of virtual.

My original concern was not with the use of virtual function. I basically feared resource loss and performance reduction because using virtual function requires declaration of multiple classes, including one base and at least one derived. null_pointer made a good point at GameDev forums that a pointer takes up very little resource. I believe he implied that even thought virtual requires declaration of multiple class objects, doing so through pointer takes up few resources.

One more question. In general, is there a performance improvement when using virtual function?

Kuphryn
 

Karpage

Ars Scholae Palatinae
725
Subscriptor
No, in fact virtual functions are a bit slower, because it has to do a table lookup to determine which function to call. Plus, its harder for the compiler to do optimization tricks like inlining, which depend on knowing at compile time which function is called.

But the slowdown is small, and its usually not worth rearranging a program to avoid it.
 

earl

Well-known member
3,066
Actually, the second part of the program works just fine -- the compiler does know which function to call. The base function. Which is almost certainly not what you want to happen. Which, if you think about it, should lead you to the point behind dynamic binding...

As for overhead, a jump to offset vs a table lookup and jump to offset isn't a big deal at all.

-earl-
 
I see the big picture is sometimes "virtual" is a necessity. It is not an option in some cases where the program has no way of knowing which function you are implying.

So the question came down to the performance of static binding and dynamic binding. If there is no performance difference, then I think it is better to just use dynamic binding with inheritance.

Kuphryn
 
-----------------
classDerived cDerived = new classDerived;

classBase *cBase = &cDerived;
-----------------

The example above comes straight from Deitel & Deitel. So everything looks okay.
WTF is that??? Not legal C++ or even close... either you didn't copy it well, or that book isn't worth its weight in paper.
The following is *not correct*:

------------------------
classBase *cDynamic = new classDerived
------------------------
It's perfectly correct (except for a missing ; ). What's wrong with it?

I don't quite get what you mean by the overhead of having a second class... you do understand that virtual is only of any use when there is more than one class involved in a hierarchy?
 
-----------------
classDerived cDerived;
classBase *cBase = cDerived;
-----------------

It looks more like that. I mixed it up, but it's the same idea.

Yes. I understand when and why one should use virtual. Many times you have the option of use either static binding or dynamic binding. I needed to know the performance reduction when using dynamic binding.

Kuphryn
 
code:<HR>
Code:
classDerived cDerived;
classBase *cBase = cDerived;
Doesn't this attempt to set the value of a pointer (cBase) to that of an object (cDerived)? :confused: I don't see that this should work (though I haven't dealt much with virtual functions & such).

I would suspect that this:
code:<HR>
Code:
classDerived cDerived;
classBase *cBase = &cDerived;
may be what you're looking for, except that euzeka responded as such:
WTF is that??? Not legal C++ or even close... either you didn't copy it well, or that book isn't worth its weight in paper.
to a pair of lines differing by one character
 

earl

Well-known member
3,066
If you have to keep asking about a choice between static and dynamic binding, I would say that you clearly don't understand the difference. In most places where dynamic binding is useful, static binding is
1- not possible
2- destroys an OO design by changing the behaviour of an object depending on what type of pointer you are using to reference it
3-would require the use of RTTI to fake dynamic binding, hence destroying any potential performance advantage

-earl-

[This message was edited by earl on December 31, 2001 at 21:27.]
 
For starter, dynamic binding should be slower. What do you think?

Well, yes, it will be slower then static dispatch, as dynamic dispatch/virtual functions abstract over either a hidden switch statement, or, as I believe it's actually implemented, a virtual function table (v-table).

However, to achieve the same results using static dispatch, you would have to accept a similar slowdown, so you don't actually lose any speed by using virtual function when necessary; the C++ compiler can probably implement a v-table better than you can, and with less drudgery and likelihood of error.

More succinctly, this comment:
3-would require the use of RTTI to fake dynamic binding, hence destroying any potential performance advantage
is especially relevant to your question.

Maybe I should just let him explain :D .


Edit:
Oh yeah:
I know the difference between static binding and dynamic binding.
They solve different problems, and static binding can't be used alone where dynamic binding is necessary or desired (see above).
 
I need to know difference in performance when using one or the other. For starter, dynamic binding should be slower. What do you think?
It is slower, but not by much. It's just an extra level of indirection. The compiler can sometimes optimize it away when the type of the object is known (it's a stack object, or was just allocated).

If you really want to see the performance difference for yourself, try it. Make a small test program which calls virtual functions and non-virtual (non-inlined) functions.

In the end it's pretty simple: if you need it, don't worry about the performance loss. There are very few cases where it'll be significant.
 
Status
Not open for further replies.