I’m sure this has something to do with virtual functions, but I’m struggling to work out how.
Here is my (simplified) situation:
Roughly what the program does is have one pair of files (computer.h) draw a computer with a blank screen, and another pair (program.h) which has a function that needs to draw on that computer screen
The computer class is going to be re-used in many different situations, so the screen draw function needs to be passed in a generic fashion
in computer.h:
include "screen.h"
class computer {
void drawComputer(); //this function draws a picture of a computer
void drawMonitor();
};
in computer.cpp:
void computer::drawComputer(){
//draws all the components then the monitor
drawMonitor(); //this is where the external function (from class screen) needs to execute
}
void computer::drawMonitor(){
//draws the background and border of screen
}
in program.h:
class program {
//many other program functions
void drawScreen();
};
in program.cpp:
//many other program functions
void program::drawScreen(){
//this function draws the contents of the screen
}
My question is, from program.cpp, how do I ‘send’ the drawScreen() function to execute within the drawMonitor() function in computer.cpp?
Edit
@Chris’ solution seems to be nearly exactly what I am after, however when I attempt to implement it I get the following errors:
testApp.h:40: error: 'testApp::prog' cannot appear in a constant-expression
testApp.h:40: error: `&' cannot appear in a constant-expression
testApp.h:40: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
testApp.h:40: error: ISO C++ forbids initialization of member 'isprog'
testApp.h:40: error: making 'isprog' static
testApp.h:40: error: invalid in-class initialization of static data member of non-integral type 'IScreen*'
testApp.h:41: error: 'isprog' has not been declared
testApp.h:42: error: ISO C++ forbids declaration of 'comp1' with no type
testApp.h:42: error: expected ';' before '.' token
The lines are
39 Program prog;
40 IScreen *isprog = dynamic_cast<IScreen*>(&prog);
41 OP1 comp1(isprog);
42 comp1.drawScreen();
Anyone know where I’m going wrong with the implementation?
Well you’re half-way there. For something like this, yes I’d use virtual functions (in order to define an abstract interface). Here’s the basic outline of how I’d do it:
Doing it this way, you can easily define multiple IScreen implementations and quickly swap them out with minimal changes to your code.
Easy, no?