Let's say I have a function drawImage
that takes a lot of arguments, some of them are of WinApi types like HWND
, HDC
and so on; and I don't want to import all of them in my class' header. But I need to call it from my class' methods.
extern void drawImage();
class A {
private :
int n;
// ...
public :
// ...
void draw() {
drawImage(n);
}
};
The code above gives me the expected error: drawImage does not take one argument
. So how could I achieve this?
Moreover, I don't want to define my member function in the source file that includes the WinApi stuff.
UPDATE:
extern void drawImage();
extern int a;
extern unsigned long int b;
...
extern "C" void c;
class A{
private :
int n;
...
public :
...
void draw(a,b,c){
drawImage(a,b,c,n);
}
}
This could make a great solution, but Visual studio does not allow extern void
declarations.
Simply define
void A::draw()
out-of-line, in a .cpp file (for exampleA.cpp
).There you can include the full the declaration of
drawImage
, and remove that include fromA.h
.If that isn't enough, that is, you want to completely separate the WinApi stuff from your code, you just have to go one step further. Your
A.cpp
would look something like:Then you'd implement
winapi::drawImage
in yet another .cpp file, saywinapi.cpp
:And
winapi.cpp
will then be the only file to know about the WinApi.Bonus point: if you compile and link with LTO enabled (
-flto
for GCC and Clang), all of this can be inlined across translation units and end up with absolutely no overhead!