Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在多个类之间传递一个静态对象?_C++_Vector_Reference_Static - Fatal编程技术网

C++ 如何在多个类之间传递一个静态对象?

C++ 如何在多个类之间传递一个静态对象?,c++,vector,reference,static,C++,Vector,Reference,Static,在我的程序中实现ImageManager时遇到问题。我成功地将此方法用于参考: //definition in Brick.h ImageManager &imgr; //constructor taking &imgr as a reference upon creation of object Brick::Brick(ImageManager &im) : imgr(im){ //imgr is now a reference in my class, so it

在我的程序中实现ImageManager时遇到问题。我成功地将此方法用于参考:

//definition in Brick.h
ImageManager &imgr;

//constructor taking &imgr as a reference upon creation of object
Brick::Brick(ImageManager &im) : imgr(im){
//imgr is now a reference in my class, so it points to the same object that imgr in another class would point to
//this essentially makes one "static" instance of imgr, so all my graphic objects are dealing with the same instance of my ImageManager
    imgr.doStuff()
}
这种传递imgr的方法过去一直有效,直到我开始尝试从向量中删除OBEJCT。例如,在我的Level类中,我尝试从砖块对象的向量中移除元素

void Level::RemoveLine(int line){
    //loop through every piece, loop through given piece's rects, if the rect falls on the removed line, then remove the piece
    for(int i = 0; i < gamePieces_.size(); i++){
        //crt new iterator per each gamepiece
        auto write = gamePieces_[i].GetPieceRectangles().begin();
        int j = 0;
        for(auto read = write; read != gamePieces_[i].GetPieceRectangles().end(); read++){
            if(gamePieces_[i].GetPieceRectangles()[j].GetActiveLine() != line){
                if(read != write){
                    write = std::move(read);
                }
                write++;
            }
        }
        gamePieces_[i].GetPieceRectangles().erase(write, gamePieces_[i].GetPieceRectangles().end());
    }
}
void Level::RemoveLine(int行){
//循环每个工件,循环给定工件的矩形,如果矩形落在移除的线上,则移除工件
对于(int i=0;i

但这不起作用,因为在
Brick.h
中声明的
ImageManager&imgr
没有复制构造函数,因此在尝试.erase()元素时无法在向量中复制它。我的目标是实现一个静态ImageManager对象,以便在我的所有类中使用。我该怎么做呢?

一般来说,这种代码不是您想要的。看看
Singleton
设计模式

“我的目标是实现一个静态ImageManager对象,以便在我的所有类中使用”

可以将ImageManager实现为单例类。但我已经学会了只有在没有其他选择的情况下才使用singleton


您还可以在类中使用静态数据成员。通过这种方式,只有一个类的数据成员副本可以流通。

如果您试图摆脱全局
图像管理器(这很好),请使用std::shared\u ptr,您可以轻松地传递它。好吧,我的尝试是使其全球化,真的,但我明白您的观点。实际上,我将参考定义
ImageManager&imgr
切换为指针
ImageManager*imgr
,并相应地更改了imgr的所有实例。这很好,还是我应该实现一个std::shared_ptr?全局是一个全局,不管它是一个单态、静态成员还是任何全局成员变量。@DieterLücking。没有确切的答案哪一个更好,但是静态方法比单例方法有一些优势。这并不能回答这个问题。若要评论或要求作者澄清,请在他们的帖子下方留下评论。实际上,这确实回答了他的问题。它指出,直接回答不是他应该追求的(即,在99.9%的情况下,直接回答是一种反模式),其次,如果他希望从任何地方以结构化方式访问单个静态实例,则有一种模式,称为“Singleton”。“这种代码不是你想要的。”听起来你在和发布问题的人争论,这表明这不是一个答案,但可能是一个批评,最好留作评论。不管怎样,@BrianCarlton是正确的-这似乎是一个链接唯一的答案,即使它不是,正如我所断言的,一个评论。