简体   繁体   English

如何取消引用存储在智能指针向量中的派生类对象?

[英]How to dereference derived class objects stored in a vector of smart pointers?

I'm building a chess game. 我正在建造一个国际象棋游戏。 I have a base class gamePiece and a derived class called rook. 我有一个基类gamePiece和一个名为rook的派生类。

I originally wanted to put all different types of pieces inside one vector and traverse it with an iterator. 我原本想把所有不同类型的碎片放在一个向量中,并用迭代器遍历它。 However, I previously discovered that I could not insert a rook object inside a vector for gamePiece objects. 但是,我以前发现我无法在gamePiece对象的向量中插入一个rook对象。

Many people recommended I instead use pointers to accomplish what I needed--smart pointers were suggested, specifically unique_ptr and shared_ptr. 许多人建议我使用指针来完成我需要的东西 - 建议使用智能指针,特别是unique_ptr和shared_ptr。 To see the discussion in my previous question, see the link below: 要查看上一个问题中的讨论,请参阅以下链接:

How to reference derived objects in a vector of pointers to base class objects? 如何在指向基类对象的指针向量中引用派生对象?

The specific technique suggested for unique_ptr is here: 为unique_ptr建议的具体技术如下:

vector<std::unique_ptr<gamePiece>> vectorOfPointersToGamePieces;
std::unique_ptr<gamePiece> rk( new rook(1, "Rook", 'A', 1, "White", "Up") );
vectorOfPointersToGamePieces.push_back(std::move(rk));

The specific technique for shared_ptr is here: shared_ptr的具体技术如下:

vector<std::shared_ptr<gamePiece>> vectorOfPointersToGamePieces;
vectorOfPointersToGamePieces.push_back(std::make_shared<rook>(1, "Rook", 'A', 1, "White", "Up") );

However, neither of these techniques seems to be working for me. 但是,这些技术似乎都不适合我。 Actually, I should clarify that a bit more--it partially works. 实际上,我应该澄清一点 - 它部分有效。 When used with "rook" (derived) objects the display function reports that my strings are empty and the variables look unitialized, even though everything in the constructors appears to be working fine. 当与“rook”(派生)对象一起使用时,显示功能报告我的字符串为空并且变量看起来是单元化的,即使构造函数中的所有内容看起来都工作正常。

Yet when these techniques are used with "gamePiece" (base) objects, the display function displays everything perfectly... So there must be a reason why this is working perfectly with base class objects, but not with derived objects. 然而,当这些技术与“gamePiece”(基础)对象一起使用时,显示功能会完美地显示所有内容......因此必须有一个理由说明它与基类对象完美配合,而不是与派生对象配合使用。

Is there some kind of step I'm missing (like do I need to cast a "rook" into a "gamePiece" before putting it in the vector...)? 是否有一些我缺少的步骤(比如我需要将“车”投入“gamePiece”才能将其放入矢量......)? I also read on another post somewhere that certain types of smart pointers can't go inside STL containers. 我还在另一篇文章中读到某些类型的智能指针不能进入STL容器内部。 Is this one of those cases? 这是其中一个案例吗? Or maybe it doesn't recognize that rook is a derived object of a gamePiece. 或者它可能不会认识到车是游戏棋子的派生对象。 That class is below. 那节课在下面。

class rook: public gamePiece
{

public:
rook(int player, string type, char file, int rank, string color, string facing)
{
    gamePiece(player, type, file, rank, color, facing);
}

};
rook(int player, string type, char file, int rank, string color, string facing)
 : gamePiece(player, type, file, rank, color, facing) {}

.. is the correct way to call a parent constructor from a derived class. ..是从派生类调用父构造函数的正确方法。 What you are doing internally is not initializing the object itself, but calling the constructor directly. 你在内部做的不是初始化对象本身,而是直接调用构造函数。 I'm surprised your compiler lets you do that. 我很惊讶您的编译器可以让您这样做。 The rook object is likely calling the default constructor of gamePiece (I assume you have something like gamePiece() {} defined in your gamepiece class). 该车对象可能正在调用gamePiece的默认构造函数(我假设您在游戏类中定义了类似gamePiece() {}的东西)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在指向基类对象的指针向量中引用派生对象? - How to reference derived objects in a vector of pointers to base class objects? 传递派生对象的智能指针矢量-最佳解决方案是什么? - Passing a vector of smart pointers of derived objects - what is the best solution? 使用智能指针将派生类的实例存储在向量中 - Using smart pointers to store instances of a derived class in a vector 派生对象/智能指针的容器 - Container of derived objects / smart pointers 制作基类指针的向量并将派生类对象传递给它(多态) - Making a vector of base class pointers and pass Derived class objects to it (Polymorphism) 派生的 class 的智能指针如何隐式转换为基本 class? - How are smart pointers of a derived class implicitly convertible to the base class? 无法取消引用插入向量中的类对象指针 - Cannot Dereference Class Object Pointers Inserted Into a Vector 如何擦除和删除指向存储在向量中的对象的指针? - How to erase & delete pointers to objects stored in a vector? 指向包含基类和派生类 class 对象的指针的向量 - 访问派生类特定变量 - Vector of pointers to base class containing base and derived class objects - accessing derived-class specific variables 使用派生模板类和智能指针进行模板推导 - Template deduction with derived templated class and smart pointers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM