1

What is the main difference between dynamic loading and dynamic binding in c++?

and why dynamic loading is called static?

MBMJ
  • 5,323
  • 8
  • 32
  • 51
  • Are you sure you choose the correct terms? dynamic loading and dynamic binding are actually the same thing. However there is a difference between static binding and dynamic binding. – Muepe Jun 22 '13 at 11:31
  • ya.i saw this term on my book – MBMJ Jun 22 '13 at 11:32
  • http://stackoverflow.com/questions/6866432/what-are-similarities-and-differences-between-dynamic-loading-and-late-binding – Jayram Jun 22 '13 at 11:41

1 Answers1

3

There are several aspects to this question. If we talk about objects we talk about dynamic binding. Lets consider the following situation

class Base {
public:
   virtual void method() { std::cout << "Base::method" << std::endl; }
};

class Derived : public Base {
public:
   virtual void method() { std::cout << "Derived::method" << std::endl; }
};

// ....

void testMethod(Base* pBase) {
   pBase->method();
}

The function call is not yet known at compilation time, it could be Base::method or Derived::method depending on the runtime type of pBase. Thats why its called dyanmic binding (or late binding), the actual method call is looked up when the call actually is about to happen.

On the other hand we have dynamic and static loading. That is related to loading libraries. One possibility is to create a static library - a file with object code - that is linked to your program when the compiler compiles and links it. It cannot be changed after compilation as its - and that's where the name comes from - statically bound to your program.

If you are going for dynamic loading however rather than adding the code to your program at compilation time you load it at runtime. Different operating systems offer various methods to do so. For the windows users DLL files shouldn't be something unknown. They contain the object code and when the code is requested by the program it loads the DLL that provides the code and executes that code. This allows you to add a different version of the DLL without having to recompile your program (as long as the signature and general behavior remains the same) - we can say its dynamic.

Muepe
  • 671
  • 3
  • 14