function resolve meaning

Hi,

I want to know that whats the meaning when we say function is statically/dynamically resolved. I actually want to know the meaning of function resolving.



Regards,
Ali
"A function is statically resolved" means that during compilation or linkage the compiler or the linker determines what function corresponds to a given name and generate the corresponding code to call the function or resolve the external reference.

"A function is dynamically resolved" means that the appropriate function that shall be called is determined during run-time of the program. Usually this takes place in two situations. The first one is when a function is called by a pointer to function. The second one is when a virtual function is called.
For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
typedef int ( *pf )( int, int );

int add( int x, int y ) { return ( x + y ); }
int multiply( int x, int y ) { return ( x * y ); }
int substruct( int x, int y ) { return ( x - y ); }
int divide( int x, int y ) { return ( x / y ); }

void execute( pf func, int x, int y )
{
   std::cout << func( x, y ) << std::endl;
}

int main()
{
   execute( add, 10, 10 );
   execute( multiply, 10, 10 );
   execute( substruct, 10, 10 );
   execute( divide, 10, 10 );

   return ( 0 );
}  


In the example the called function inside function execute is resolved dynamically and depends on the value of the first parameter.

Another example with virtual functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
strcut A
{
   virtual void f() const {}
   virtual ~A() {}
};

struct B : A
{
   void f() const {}
};

struct C : A
{
   void f() const {}
};

int main()
{
   A *pa1 = new B;
   pa1->f();

   A *pa2 = new C;
   pa2->f();

   delete pa1;
   delete pa2;

   return 0;
}



Here the static type of function call expressions pa1->f() and pa2->f() is A::f. However the appropriate function will be resolved dynamically at run time and instead of A::f will be called either B::f or C::f.
Last edited on
Topic archived. No new replies allowed.