Zak Henry
Zak Henry

Reputation: 2185

c++ classes - how do I pass a function to an included class for it to use?

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?

Upvotes: 2

Views: 238

Answers (3)

Chris Eberle
Chris Eberle

Reputation: 48795

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:

// First create a class to define an interface for drawing a screen
class IScreen
{
public:
    // Defines an interface named drawScreen
    virtual void drawScreen() = 0;
};

// Next actually implement the interface
class Program : public IScreen
{
public:
    // Here we actually implement it
    virtual void drawScreen()
    {
        // Draw some stuff here
    }
};

// You can implement this more than once if you want
class BlueScreenOfDeathScreen : public IScreen
{
public:
    virtual void drawScreen()
    {
        // Draw a BSOD on the screen
    }
};

// Finally, use the interface
class Computer
{
private:
    IScreen* myScreen;

public:
    Computer(IScreen* screen)
        : myScreen(screen)
    {
    }

    void drawComputer()
    {
        // ...
    }

    void drawMonitor()
    {
        // Draw the monitor
        // ...

        // Draw the screen
        myScreen->drawScreen();
    }
};

Doing it this way, you can easily define multiple IScreen implementations and quickly swap them out with minimal changes to your code.

// Render using the "Program" class
Program prog;
IScreen *iprog = dynamic_cast<IScreen*>(&prog);
Computer comp1(iprog);
comp1.drawScreen();

// Render using the "BlueScreenOfDeathScreen" class
BlueScreenOfDeathScreen bsod;
IScreen *ibsod = dynamic_cast<IScreen*>(&bsod);
Computer comp2(ibsod);
comp2.drawScreen();

Easy, no?

Upvotes: 3

iammilind
iammilind

Reputation: 70048

The most straight forward answer is to have an object of program inside computer and call drawScreen() from within. i.e.

class computer {
  program prg;  // <--- internal object
//...
};
void computer::drawMonitor() {
  prg.drawScreen();
}

Another way is to pass an reference object of program to drawMonitor and call the method. i.e.

void computer::drawMonitor(program &prg) {  // <--- pass by reference
  prg.drawScreen();
}

Upvotes: 0

Tony The Lion
Tony The Lion

Reputation: 63250

You'd have to create an instance of program in your computer class, like so:

Program mypgm;

and then within your

void computer::drawMonitor(){
    //draws the background and border of screen
   mypgm.DrawScreen();

}

Upvotes: 1

Related Questions