从移交的类调用对象
Calling Object from a handed over class
你能帮我如何在移交对象的类后调用类中的对象吗?
所以我有一个游戏.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> ¶ms);
};
我尝试过这样的东西,但我不会:(工作:构建.cpp
int Build::execute(Sep::Game &game, std::vector<std::string> ¶ms)
{
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> ¶ms) = 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> ¶ms) override;
};
}
构建.cpp:
#include "Build.h"
namespace Sep
{
int Build::execute(Sep::Game &game, std::vector<std::string> ¶ms)
{
if(params.size() == 4 )
{
game.Street.SomeFunction();
}
}
}
也许这可以帮助您弄清楚。
相关文章:
- 是什么让放置新调用对象的构造函数?
- 当我调用对象的方法时,对象的成员会发生变化
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- 如何从构造函数副本 T(const T&)调用对象 T?
- C++17 如何保存泛型可调用对象以供以后使用
- 检查模板中 nullptr 的函数指针,了解任何类型的可调用对象
- 是否可以在编译时初始化对象的 C 样式函数指针,以便它调用对象的成员函数?
- 如何在C++中将可调用对象放入地图中?
- 在销毁期间从另一个线程调用对象上调用方法是否未定义行为?
- 广义 std::function (std::any 表示可调用对象)
- c++ 替换调用对象方法的宏函数
- 将可调用对象传递给采用 std::function 的构造函数
- 双指针在使用 new 时不调用对象构造函数
- 可调用对象作为默认模板参数
- 只需调用对象即可获取对象数据
- 如果用户尝试从 JS 调用对象的未定义函数C++则回调C++代码
- 如何将当前替代类型的 std::variant 传递给可调用对象?
- C++对象本身作为参数调用对象的方法
- 从移交的类调用对象