What is the main difference between dynamic loading and dynamic binding in c++?
and why dynamic loading is called static?
What is the main difference between dynamic loading and dynamic binding in c++?
and why dynamic loading is called static?
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.