【问题标题】:Call by reference with QVector使用 QVector 通过引用调用
【发布时间】:2010-05-03 15:50:52
【问题描述】:

我在一个对象中有一个坐标的 QVector(我的类型),我想将它转移到另一个向量(我验证并且不想使用 ist)。

标题

bool getVector(QVector<Coordinates> &getCoordinates );

C 文件

static QVector<Coordinates> current;


int getVector( QVector<Coordinates> &getCoordinates)
{

.... stuff ...

 getCoordinates = current;

.... stuff ....
return 0;
}

我喜欢使用它

....

QVector<Coordinates> currentCoordinates;
getVector(currentCoordinates);

currentCoordinates.X // CRASH

调试器转到发生 Live Crash 的这一行

  inline QVector(const QVector<T> &v) : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }

那么我该如何解决这个问题?因为我可以使用这个方法来获取所有其他变量。

【问题讨论】:

    标签: qt reference vector


    【解决方案1】:

    您的问题的一个可能原因是在调用 getVector 之前尚未构造 current。 C++ 中静态对象的初始化是一个棘手的领域,也是常见的错误来源 - 有关详细信息,请参阅 this questionthe static initialization order fiasco FAQ entry

    解决此问题的一个简单方法是通过函数提供对current 的访问,即替换

    static QVector<Coordinates> current;
    

    static QVector<Coordinates>& getCurrent()
    {
        static QVector<Coordinates> current;
        return current;
    }
    

    但是请注意,上面编写的函数不是线程安全的。如果多个线程可能调用getCurrent,那么应该使用QMutex 进行保护。

    【讨论】:

    • 我照你说的做了。它工作得很好。但我被告知,如果我只是将 QVector 更改为自己的类型,如 typdef QVector VesselCoordinates,我应该可以工作。它也可以正常工作。即使我正在删除您的功能,它仍然可以正常工作。谢谢
    • @Thomas,仅仅使用 typedef 根本不应该改变程序的行为——它只是编译器的“语法糖”。您确定只需在原始程序中添加 typedef 即可修复崩溃吗?
    • 是的,只是 typedef 修复了它。正如我被告知的那样,它使编译器及时解决了它,然后构造了 Vector。我们欺骗了编译器。
    【解决方案2】:

    对于 gareth 和论坛:

    标题:

    typedef  QVector<Coordinates> VesselCoordinates;
    
      bool (*getVessel)(Charakter forCharakter, Vessel& getVessel,VesselCoordinates &getCoordinates );
    

    稍后我将 tis 函数指针绑定到一个静态函数(因为我的这部分程序有一天会转换为 c)

    cpp文件下层:

         static struct {
                Charakter currentPlayerVessel;
                VesselCoordinates possibility;
            }data;
             static bool getVessel(Charakter forCharakter, Vessel& getVessel,VesselCoordinates &getCoordinates );
    
    // funktion to bind the funktion pointer to this static funktion so it can be called outside the File
    
            static bool serverNamespace::getVessel(Charakter forCharakter, Vessel& getVessel,VesselCoordinates &getCoordinates )
            {
                bool retValue= false;
    
                if ( forCharakter == data.currentPlayerVessel){
                    // TODO abfragen ob die Adresse regestriert ist!
                    if ((true == minSize()) and  ((true == shipsInRow())or (true == shipsInLine())))
                {
                    retValue = true;
                    Vessel test = (Vessel)data.possibility.size();
                    getVessel =  test;
                    getCoordinates = data.possibility;
                }
            }
            return retValue;
        }
    

    然后我可以在上层cpp文件中使用这个来获取我需要的信息:

    // in an Funktion : 
    
     VesselCoordinates currentCoordinates;
        currentCoordinates.clear();
    
        Vessel currentVessel;
    
    if (true == basicFleet->getVessel(currentCharakter,currentVessel, currentCoordinates ))
    
    // doing stuff to it 
    

    所以它工作得很好,但你的想法同样有效。也许你能明白为什么我的想法也奏效了。

    谢谢

    电极

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      相关资源
      最近更新 更多