1)How to call a C++ function which is compiled with C++ compiler in C code?
Solution: The C++ compiler must know that f(int,char,float) is to be called by a C compiler using the extern "C"construct:
The extern "C" line tells the compiler that the external information sent to the linker should use C calling conventions and name mangling (e.g., preceded by a single underscore). Since name overloading isn't supported by C, you can't make several overloaded functions simultaneously callable by a C program.
// This is C++ code
// Declare f(int,char,float) using extern "C":
extern "C" void f(int i, char c, float x);
// Define f(int,char,float) in some C++ module:
void f(int i, char c, float x)
2)When you deliver your C++ headers and C++ library of a class (what all can you change in the class so that application using your class does not need to recompile the code)
3)How do you initialize a static member of a class with return value of some function?
Static data members are shared by all object instances of that class. Each class instance sees and has access to the same static data. The static data is not part of the class object but is made available by the compiler whenever an object of that class comes into scope. Static data members, therefore, behave as global variables for a class. One of the trickiest ramifications of using a static data member in a class is that it must be initialized, just once, outside the class definition, in the source file. This is due to the fact a header file is typically seen multiple times by the compiler. If the compiler encountered the initialization of a variable multiple times it would be very difficult to ensure that variables were properly initialized. Hence, exactly one initialization of a static is allowed in the entire program.
Consider the following class, A, with a static data member, _id:
The initialization of a static member is done similarly to the way global variables are initialized at file scope, except that the class scope operator must be used. Typically, the definition is placed at the top of the class source file:
// File: a.cc
Because no explicit initial value was specified, the compiler will implicitly initialize _id to zero. An explicit initialization can also be specified:
// File: a.cc
int A::_id = 999;
In fact, C++ even allows arbitrary expressions to be used in initializers:
// File: a.cc
int A::_id = GetId();
4)How can one application use same API provided by different vendors at the same time?
5)If you are given the name of the function at run time how will you invoke the function?
* Compile your program with --export-dynamic on the gcc command line
* Link with -ldl (dynamic library handling, needed for dlopen and dlsym
* Call dlopen() with a null parameter, meaning you aren't loading symbols from a file but from the current executable
* Call dlsym() with the name of the function you'll be calling. Note that C++ modifies function names, so If you're trying this with C++, you'd have to either declare this function as extern "C", or figure out what name the function has after compiling. (On unix, you could run nm -s on the object file for this).
* If dlsym() returns non-null, use the returned value as a function pointer to invoke your function.
I didnt prepare all the answers please try to answer if you know and comment it