开发者

Syntax for const accessor by reference

开发者 https://www.devze.com 2022-12-27 05:23 出处:网络
Right now my implementation returns the thing by value. The member m_MyObj itself is not const - its value changes depending on what the user selects with a Combo Box. I am no C++ guru, but I want to

Right now my implementation returns the thing by value. The member m_MyObj itself is not const - its value changes depending on what the user selects with a Combo Box. I am no C++ guru, but I want to do this right. If I simply stick a & in front of GetChosenSourceSystem in both decl. and impl., I get one sort of compiler error. If I do one but not another - another error. If I do return &m_MyObj;. I will not list the errors here for now, unless there is a strong demand for it. I assume that an experienced C++ coder can tel开发者_JAVA技巧l what is going on here. I could omit constness or reference, but I want to make it tight and learn in the process as well.

// In header file
MyObj GetChosenThingy() const;

// In Implementation file.
MyObj MyDlg::GetChosenThingy() const
{
    return m_MyObj;
}


The returned object will have to be const, so you cant change it from outside;

// In header file
const MyObj& GetChosenThingy() const;

// In Implementation file.
const MyObj& MyDlg::GetChosenThingy() const
{
    return m_MyObj;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号