将类的实例传递到类中
Pass instance of class into Class
本文关键字:实例 更新时间:2023-10-16
我需要将一个类的不同实例传递到该类中,以便我可以在里面访问
Player Player1(Player2);
Player Player2(Player1);
但是,由于尚未创建 P2,因此它不起作用。或者有没有更好的方法在 P2 内部访问 P1?
没错。玩家 1 如何知道玩家 2,因为它不是在创建玩家 1 时创建的。最好在 Player 类中使用 setter 方法。
例子是。
class Player
{
public:
Player();
setPlayer(Player* player)
{
otherPlayer = player;
}
private:
Player* otherPlayer;
};
然后你可以称这些为。
Player Player1;
Player Player2;
Player1.setPlayer(&Player2);
Player2.setPlayer(&Player1);
您可以传递稍后将在其中创建 Player2 的指针:
/*** class declaration ***/
class Player
{
private:
class Player **ref; // pointer to pointer of reference Player class
bool all_done; // flag to indicate whether the whole initialization procedure is finished
public:
Player1(class **RefPtr); // constructor declaration
void DoRemainingInit(); // if necessary, perform remaining initialization steps
}
。
/*** class method definitions ***/
Player::Player(class **RefPtr):
ref(NULL), all_done(false)
{
// ...
if (RefPtr == NULL)
{
// pointer invalid -> do some error handling here
return;
}
ref = RefPtr;
if (*ref == NULL)
{
// pointer is valid, but doesn't yet point to another class instance -> try to access it later by calling DoRemainingInit()
// ...
}
else
{
// pointer is valid and should point to an existing object (depends on whether RefPtr was properly initialized with NULL outside) -> do your stuff here
all_done = true;
// ...
}
}
void Player::DoRemainingInit()
{
if (all_done)
{
// nothing to be done here -> return immediately
return;
}
if (*ref == NULL)
{
// reference object not yet existent -> possibly treat as error?
// ...
}
else
{
// reference object is now existent -> do the same stuff as in the constructor's "else" branch
all_done = true;
// ...
}
}
。
/*** creating the objects ***/
class *Player1 = NULL; // WARNING: MUST be set to NULL initially, otherwise the check inside Player's constructor won't work correctly
class *Player2 = NULL; // WARNING: MUST be set to NULL initially, otherwise the check inside Player's constructor won't work correctly
Player1 = new Player(&Player2);
Player2 = new Player(&Player1);
Player1->DoRemainingInit();
Player2->DoRemainingInit();
return; // WARNING: "ref" inside both classes is not valid any more, once we return here!
请注意,"ref"属性实际上指向"Player2"指针本身,因此一旦"Player2"指针的范围留在Playeyou内部,也不能立即在Player1的构造函数中Player2,则不再有效。您可能还希望通过使引用恒定来限制对引用的访问。
另请注意,上面的代码是示例代码,因此缺乏一些基本的安全检查(例如,我们不知道"new"是否能够实际创建一个类实例)。
相关文章:
- 从C++实例化QML
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 建议在运行时将带有类实例的列表从c++导入qml
- 约束和显式模板实例化
- 通过实例理解std::move及其目的
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 正在生成未知类实例
- 从DLL中删除类的实例
- 在std::vector上存储带有模板的类实例
- 为什么创建友元类的实例会导致"undefined reference to"错误?