How one can achieve late binding in C language ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Late binding is not really a function of the C language itself, more something that your execution environment provides for you.
Many systems will provide deferred binding as a feature of the linker/loader and you can also use explicit calls such as
dlopen(to open a shared library) anddlsym(to get the address of a symbol within that library so you can access it or call it).The only semi-portable way of getting late binding with the C standard would be to use some trickery with
system()and even that is at least partially implementation-specific.If you’re not so much talking about deferred binding but instead polymorphism, you can achieve that effect with function pointers. Basically, you create a
structwhich has all the data for a type along with function pointers for locating the methods for that type. Then, in the “constructor” (typically an init() function), you set the function pointers to the relevant functions for that type.You still need to include all the code even if you don’t use it but it is possible to get polymorphism that way.