I'll start out with some context.
我将从一些背景开始。
Making a simple game.
制作一个简单的游戏。
I have two classes, one called BouncingBall, and the other called ASCIIRenderer. I have a list of instructions to follow, but some of the instructions aren't entirely clear.
我有两个类,一个叫做BouncingBall,另一个叫做ASCIIRenderer。我有一份要遵循的说明列表,但有些说明并不完全清楚。
First instruction was to create a pointer in BouncingBall called m_pRenderer, and have it point to a member variable in ASCIIRenderer. It wasn't specified which member variable I had to point to, and both existing member variables in there were private, so I made my own and called it Renderer.
第一条指令是在BouncingBall中创建一个名为m_pRenderer的指针,并指向ASCIIRenderer中的成员变量。没有指定我必须指向哪个成员变量,并且那里的现有成员变量都是私有的,所以我自己制作并称之为Renderer。
Second instruction (the one I need help with) is when I'm writing a function for the BouncingBall class to call SetPixel using the m_pRenderer, and with three variables as parameters.
第二条指令(我需要帮助的)就是当我为BouncingBall类编写一个函数时,使用m_pRenderer调用SetPixel,并使用三个变量作为参数。
SetPixel is the name of a public function in the ASCIIRenderer class, and the instruction states I have to call it by using the pointer somehow.
SetPixel是ASCIIRenderer类中的公共函数的名称,并且指令状态我必须通过某种方式使用指针来调用它。
Summary: I need to call a class' function from within the function of a separate class using a pointer.
简介:我需要使用指针从单独的类的函数内调用类的函数。
Could someone explain to me what syntax I would use to accomplish this?
有人可以向我解释一下我用什么语法来实现这个目的吗?
2 个解决方案
#1
1
Based on the details you provided this is what I gathered. Assuming the BouncingBall class would get the X and Y pos and call the function foo with the X and Y values to have the Renderer set. Also, I don't know how you will initialize the pointer as it's not detailed above. Hope this helps.
根据您提供的详细信息,这是我收集的内容。假设BouncingBall类将获得X和Y pos并使用X和Y值调用函数foo以设置Renderer。另外,我不知道你将如何初始化指针,因为它没有在上面详述。希望这可以帮助。
class BouncingBall
{
public:
void foo( int posX, int posY)
{
m_pRenderer->setPixel( posX, posY);
}
private:
ASCIIRenderer* m_pRenderer;
};
#2
0
The code I have included below shows an example of a class, in this case Ball
, having a pointer to another class, in this case Renderer
, injected and stored for later use.
我在下面包含的代码显示了一个类的示例,在本例中为Ball,具有指向另一个类的指针,在本例中为Renderer,注入并存储以供以后使用。
The Ball
class can then call public functions on the Renderer
class using the 'arrow' syntax pointer->MemberFunction(arg1, arg2);
然后Ball类可以使用'箭头'语法指针 - > MemberFunction(arg1,arg2)调用Renderer类上的公共函数;
#include <iostream>
class Renderer
{
public:
/* Constructor and other functions omitted */
void SetPixel(const int x, const int y) const;
};
void Renderer::SetPixel(const int x, const int y) const
{
std::cout << "Set pixel: (" << x << ", " << y << ")" << std::endl;
}
class Ball
{
public:
Ball(const Renderer *const renderer, const int posX, const int posY);
void Render() const;
private:
const Renderer* _renderer;
int _posX;
int _posY;
};
Ball::Ball(const Renderer *const renderer, const int posX, const int posY)
: _renderer(renderer), _posX(posX), _posY(posY)
{}
void Ball::Render() const
{
_renderer->SetPixel(_posX, _posY);
}
int main()
{
const Renderer renderer;
const Ball ball(&renderer, 10, 20);
ball.Render();
return 0;
}
The output of the program is: Set pixel: (10, 20)
程序的输出是:设置像素:(10,20)
#1
1
Based on the details you provided this is what I gathered. Assuming the BouncingBall class would get the X and Y pos and call the function foo with the X and Y values to have the Renderer set. Also, I don't know how you will initialize the pointer as it's not detailed above. Hope this helps.
根据您提供的详细信息,这是我收集的内容。假设BouncingBall类将获得X和Y pos并使用X和Y值调用函数foo以设置Renderer。另外,我不知道你将如何初始化指针,因为它没有在上面详述。希望这可以帮助。
class BouncingBall
{
public:
void foo( int posX, int posY)
{
m_pRenderer->setPixel( posX, posY);
}
private:
ASCIIRenderer* m_pRenderer;
};
#2
0
The code I have included below shows an example of a class, in this case Ball
, having a pointer to another class, in this case Renderer
, injected and stored for later use.
我在下面包含的代码显示了一个类的示例,在本例中为Ball,具有指向另一个类的指针,在本例中为Renderer,注入并存储以供以后使用。
The Ball
class can then call public functions on the Renderer
class using the 'arrow' syntax pointer->MemberFunction(arg1, arg2);
然后Ball类可以使用'箭头'语法指针 - > MemberFunction(arg1,arg2)调用Renderer类上的公共函数;
#include <iostream>
class Renderer
{
public:
/* Constructor and other functions omitted */
void SetPixel(const int x, const int y) const;
};
void Renderer::SetPixel(const int x, const int y) const
{
std::cout << "Set pixel: (" << x << ", " << y << ")" << std::endl;
}
class Ball
{
public:
Ball(const Renderer *const renderer, const int posX, const int posY);
void Render() const;
private:
const Renderer* _renderer;
int _posX;
int _posY;
};
Ball::Ball(const Renderer *const renderer, const int posX, const int posY)
: _renderer(renderer), _posX(posX), _posY(posY)
{}
void Ball::Render() const
{
_renderer->SetPixel(_posX, _posY);
}
int main()
{
const Renderer renderer;
const Ball ball(&renderer, 10, 20);
ball.Render();
return 0;
}
The output of the program is: Set pixel: (10, 20)
程序的输出是:设置像素:(10,20)