从内部对象访问外部对象
Access outer object from inner object
我有 2 个(不完整的)classes,级别和对象,在不同的文件中,看起来像这样
Object.h:
#pragma once
#include "Core.h"
class Object
{
public:
Object(const Hitbox &hBox_, const Vector2& position_ = Vector2{0, 0}, const Vector2& velocity_ = Vector2{ 0, 0 });
virtual Hitbox getHitbox();
virtual void update();
virtual void draw();
virtual void drawHbox(const SDL_Color& color = SDL_Color{255, 0, 0, 255});
static Core* core;
protected:
Vector2 position;
Vector2 velocity;
Hitbox hBox;
};
Level.h:
#pragma once
#include "Object.h"
class Level
{
public:
Level(const Vector2 &size_);
void proceed();
bool checkStaticCollision(Object* self);
Object* checkBulletCollision(Object* self);
protected:
std::vector<std::shared_ptr<Object>> objects;
Vector2 size;
};
关卡保留所有对象,对象可以做一些独立的事情,比如绘制自己或接收输入,但对象也必须相互交互,做一些关卡的事情,比如碰撞、创建和删除自己、发射粒子等。我怎样才能让对象访问级别?不完整的类型不提供所需的功能,所以我不能只在 Object.h 中使用 class Level;
,我不能简单地保留指向所需函数的指针,因为它们是 class 方法,如果我保留它会很棒它没有 void*.
不确定这是否是最好的解决方案,但是,感谢@PeteBecker,我已经添加(并且还发现了,呵呵)包括保护所以现在看起来像这样:
Object.h:
#include "Core.h"
#ifndef OBJECT_H_
#define OBJECT_H_
#include "Level.h"
class Level;
class Object
{
public:
Object(Level* level_, const Hitbox &hBox_, const Vector2& position_ = Vector2{0, 0}, const Vector2& velocity_ = Vector2{ 0, 0 });
virtual Hitbox getHitbox();
virtual void update();
virtual void draw();
virtual void drawHbox(const SDL_Color& color = SDL_Color{255, 0, 0, 255});
static Core* core;
protected:
Vector2 position;
Vector2 velocity;
Level *level;
Hitbox hBox;
};
#endif
Level.h:
#include "Core.h"
#ifndef LEVEL_H_
#define LEVEL_H_
#include "Object.h"
class Object;
class Level
{
public:
Level(const Vector2 &size_);
void proceed();
bool checkStaticCollision(Object* self);
Object* checkBulletCollision(Object* self);
protected:
std::vector<std::shared_ptr<Object>> objects;
Vector2 size;
};
#endif
我有 2 个(不完整的)classes,级别和对象,在不同的文件中,看起来像这样
Object.h:
#pragma once
#include "Core.h"
class Object
{
public:
Object(const Hitbox &hBox_, const Vector2& position_ = Vector2{0, 0}, const Vector2& velocity_ = Vector2{ 0, 0 });
virtual Hitbox getHitbox();
virtual void update();
virtual void draw();
virtual void drawHbox(const SDL_Color& color = SDL_Color{255, 0, 0, 255});
static Core* core;
protected:
Vector2 position;
Vector2 velocity;
Hitbox hBox;
};
Level.h:
#pragma once
#include "Object.h"
class Level
{
public:
Level(const Vector2 &size_);
void proceed();
bool checkStaticCollision(Object* self);
Object* checkBulletCollision(Object* self);
protected:
std::vector<std::shared_ptr<Object>> objects;
Vector2 size;
};
关卡保留所有对象,对象可以做一些独立的事情,比如绘制自己或接收输入,但对象也必须相互交互,做一些关卡的事情,比如碰撞、创建和删除自己、发射粒子等。我怎样才能让对象访问级别?不完整的类型不提供所需的功能,所以我不能只在 Object.h 中使用 class Level;
,我不能简单地保留指向所需函数的指针,因为它们是 class 方法,如果我保留它会很棒它没有 void*.
不确定这是否是最好的解决方案,但是,感谢@PeteBecker,我已经添加(并且还发现了,呵呵)包括保护所以现在看起来像这样:
Object.h:
#include "Core.h"
#ifndef OBJECT_H_
#define OBJECT_H_
#include "Level.h"
class Level;
class Object
{
public:
Object(Level* level_, const Hitbox &hBox_, const Vector2& position_ = Vector2{0, 0}, const Vector2& velocity_ = Vector2{ 0, 0 });
virtual Hitbox getHitbox();
virtual void update();
virtual void draw();
virtual void drawHbox(const SDL_Color& color = SDL_Color{255, 0, 0, 255});
static Core* core;
protected:
Vector2 position;
Vector2 velocity;
Level *level;
Hitbox hBox;
};
#endif
Level.h:
#include "Core.h"
#ifndef LEVEL_H_
#define LEVEL_H_
#include "Object.h"
class Object;
class Level
{
public:
Level(const Vector2 &size_);
void proceed();
bool checkStaticCollision(Object* self);
Object* checkBulletCollision(Object* self);
protected:
std::vector<std::shared_ptr<Object>> objects;
Vector2 size;
};
#endif