gpt4 book ai didi

c++ 类 - 如何将函数传递给包含的类以供其使用?

转载 作者:搜寻专家 更新时间:2023-10-31 01:18:38 26 4
gpt4 key购买 nike

我确定这与虚函数有关,但我正在努力弄清楚如何做。

这是我的(简化的)情况:

该程序的大致作用是让一对文件 (computer.h) 绘制一台黑屏计算机,另一对文件 (program.h) 具有需要在该计算机屏幕上绘制的功能

计算机类将在许多不同的情况下被重用,因此需要以通用方式传递屏幕绘制函数

computer.h 中:

include "screen.h"

class computer {

void drawComputer(); //this function draws a picture of a computer
void drawMonitor();

};

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
}

program.h中:

class program {

//many other program functions

void drawScreen();

};

program.cpp 中:

//many other program functions

void program::drawScreen(){
//this function draws the contents of the screen
}

我的问题是,在 program.cpp 中,我如何“发送”drawScreen() 函数以在 drawMonitor() 中执行> computer.cpp 中的函数?

编辑

@Chris 的解决方案似乎几乎正是我所追求的,但是当我尝试实现它时,出现以下错误:

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

线条是

39    Program prog;
40 IScreen *isprog = dynamic_cast<IScreen*>(&prog);
41 OP1 comp1(isprog);
42 comp1.drawScreen();

有人知道我在实现方面哪里出了问题吗?

最佳答案

好吧,你已经完成了一半。对于这样的事情,是的,我会使用虚函数(为了定义一个抽象接口(interface))。以下是我将如何做的基本概述:

// 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();
}
};

通过这种方式,您可以轻松地定义多个 IScreen 实现,并通过对代码进行最少的更改来快速交换它们。

// 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();

很简单,不是吗?

关于c++ 类 - 如何将函数传递给包含的类以供其使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6908051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com