header code. ?The C++ Compiler will compile and link without any
problems if one function is defined outside of header code.
For example:
// Func.h header
void ReadWrite(); // function phototype
void Func1() {}
void Func2() {}
void Func3() {}
void Run()
This should be:
inline void Run()
{
? ? ?Func1();
? ? ?Func2();
? ? ?Func3();
? ? ?ReadWrite();
}
// main.cpp source code
#include "Func.h"
void ReadWrite() // define ReadWrite() here
{
}
int main()
{
? ? ?Run();
? ? ?return 0;
}
You can see what I mean. ?Func1(), Func2(),Func3(), and Run() are
always unmodified in the .lib or .dll. ?You want to modify
ReadWrte() in main.cpp source code.
If I understand correctly, you want to call Run() from main.cpp and
you want that Run() would call back another function in main.cpp.
So this is essentially a callback mechanism. In C you would pass a
callback function pointer to the Run() function. In C++ you can use
templates instead, with the benefit that in addition to a plain
function you can pass a functor object holding some state, when
needed.
template<typename CALLBACK>
void Run(CALLBACK callback) {
? ? Func1();
? ? Func2();
? ? Func3();
? ? callback();
}
...
int main()
{
? ? ?Run(ReadWrite);
? ? ?return 0;
}
If you forget to define ReadWrite(), then C++ Compiler succeeds to
compile, but it fails to link. ?
This is a good thing, isn't it?
Please suggest the best practice how .lib and .dll can call
ReadWrite() in main.cpp source code.
Your example concerned calling ReadWrite() from a function in an
exported header file. This is not inside .lib or .dll, which would be
a totally different thing.
If the function was indeed defined inside a lib or dll, the template
approach would not work (without support to template export, which is
not generally implemented). Instead I would go with defining an
abstract base class in the library header, providing a derived class
with needed overridden function in the main.cpp and passing a
reference of the derived class object to the Run() function. In my
experience passing a plain function pointer as a callback almost
never suffices, the function almost always needs some context or
state from the calling site to operate properly. And no, do not even
mention global variables!
Thanks for explanation how template works. I understand that you
can't bind ReadWrite() into Run(). Please give me example of class
code. You should define Run() in base class. Put pure virtual
ReadWrite() in the same base class. Then, base class is inside .lib
or .dll.
You write to include base class header code. You add second class to
be derived from base class. Then, you can add ReadWrite() inside
derived class. It overrides base class' ReadWrite(). You can use
base class' Run() inside main(). Run() from base class will call
ReadWrite() in derived class.