如何将Int32 *转换为Int32?

时间:2016-12-08 09:35:31

标签: c++

这是我的代码

int32 *iPos;
status =scriptData.GetInt32(iPos);
int32 position=iPos;

但它出现error C2440: 'initializing' : cannot convert from 'int32 *' to 'int32'

如何将Int32 *转换为Int32?

3 个答案:

答案 0 :(得分:2)

只需取消引用它:

int32 position = *iPos;

但更优雅的版本是使用&运算符来获取指向position的指针:

int32 position;
status = scriptData.GetInt32(&position);

答案 1 :(得分:0)

也许你应该这样做:

int32 position = *ipos;

(* ipos)返回指针的值,(& ipos)返回指针的地址

答案 2 :(得分:0)

你显然应该尝试下一个Int32指针工厂:

class Int32Factory {
public:
    static Int32Factory *instance() {
        static std::unique_ptr<Int32Factory> s_s(new Int32Factory());
        return s_s.get();
    }

    ~Int32Factory() {
        for (auto i : m_items) {
            delete i;
        }
    }

    int32 *getInt() {
        m_items.push_back(new int32());
        return m_items.back();
    }

private:
    std::vector<int32 *> m_items;

    Int32Factory() { }
};

使用简单,只需执行后续步骤:

int32 *iPos = Int32Factory::instance()->getInt();
status = scriptData.GetInt32(iPos);