从移交的类调用对象

Calling Object from a handed over class

本文关键字:调用 对象      更新时间:2023-10-16

你能帮我如何在移交对象的类后调用类中的对象吗?

所以我有一个游戏.cpp其中构造函数我创建所有对象。

游戏.cpp

//CONSTRUCTOR
Game::Game(Sep::Interface &io, std::string config): config_(config)
{
... some other things ..
//creating objects
Sep::Property Obstacle(Sep::Field::FieldType::WATER,"OBSTACLE", 'O', true, false, 10, 20);
Sep::Property Street(Sep::Field::FieldType::WATER,"STREET", 'S', true, false, 20, 10);
};

在这里,您可以看到它是构造器游戏.h

class Game
{
public:
//------------------------------------------------------------------------
// Game constructor & destructor
//
Game(Sep::Interface &io, std::string config);
~Game() noexcept;
};

现在我有一个类构建,它有一个方法执行,它有参数类游戏和一些参数作为矢量。

我的问题如何从给定的游戏类参数调用对象的方法?

构建.h

class Game;
class Build : public Command
{
public:
//------------------------------------------------------------------------
// Constructor
Build(){};
//------------------------------------------------------------------------
// Destructor
~Build() noexcept ;
int execute(Game &game, std::vector<std::string> &params);
};

我尝试过这样的东西,但我不会:(工作:构建.cpp

int Build::execute(Sep::Game &game, std::vector<std::string> &params)
{
if(params.size() == 4 )
{
Street s;
// OR
game Streets s;
}
}

好的,试一试,但我不能 100% 确定这是否真的是你的问题:

游戏.h:

template <typename T> void USE(const T&) {}
namespace Sep
{
namespace Field
{
enum class FieldType { WATER };
}
class Property
{
public:
Property(Field::FieldType, const char*, char, bool, bool, int,int);
void SomeFunction();
};
class Interface;
}
class Command {
virtual int execute(Game &game, std::vector<std::string> &params) = 0;
};
#include <string>
namespace Sep{
class Game
{
public:
//------------------------------------------------------------------------
// Game constructor & destructor
//
Game(Sep::Interface &io, std::string config);
~Game() noexcept;
std::string config_;
Sep::Property Obstacle;
Sep::Property Street;

};
} // namespace Sep

游戏.cpp

namespace Sep
{
Game::Game(Sep::Interface &io, std::string config)
: config_(config)
, Obstacle(Sep::Field::FieldType::WATER,"OBSTACLE", 'O', true, false, 10, 20)
, Street(Sep::Field::FieldType::WATER,"STREET", 'S', true, false, 20, 10)
{
USE(io);
}
}

构建.h

#include "Game.h"
#include <vector>
namespace Sep
{
class Build : public Command
{
public:
//------------------------------------------------------------------------
// Constructor
Build(){};
//------------------------------------------------------------------------
// Destructor
~Build() noexcept ;
virtual int execute(Game &game, std::vector<std::string> &params) override;
};
}

构建.cpp:

#include "Build.h"
namespace Sep
{
int Build::execute(Sep::Game &game, std::vector<std::string> &params)
{
if(params.size() == 4 )
{
game.Street.SomeFunction();
}
}
}

也许这可以帮助您弄清楚。