加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C访问类内的vector值

发布时间:2020-12-16 10:33:16 所属栏目:百科 来源:网络整理
导读:参见英文答案 Accessing vector in class????????????????????????????????????3个 我有以下课程: class clsUnitArrayIndexToUnitID : public CBaseStructure{ private: std::vectorint m_content; long m_size; protected: void ProcessTxtLine(string lin
参见英文答案 > Accessing vector in class????????????????????????????????????3个
我有以下课程:

class clsUnitArrayIndexToUnitID : public CBaseStructure
{
    private:
        std::vector<int> m_content;
        long m_size;
    protected:
        void ProcessTxtLine(string line);
    public:
        clsUnitArrayIndexToUnitID();
        std::vector<int>* Content;
        long Size();
};

我想从类外部访问值,例如:

int iUnitID = m_MyClass.Content()[12];

但是,C告诉我,我需要使用点到函数类型.
我不确定那意味着什么.

另外,如果有人在我的代码中看到任何缺陷,请告诉我.

解决方法

将其更改为功能(根据您的需要调整const)

public:
     const std::vector<int>& Content() const { return m_content; }

并按照您的描述使用,或以任一方式取消引用指针(不安全?):

m_MyClass.Content->at(12); 
(*m_MyClass.Content).at(12);
(*m_MyClass.Content)[12];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读