Using references to access class objects C++

三世轮回 提交于 2019-12-02 11:51:11

This is where you went wrong

CClassWrap::InitWrap( CClass AppIfx )
{
    PlotArgs = AppIfx.PlotArgs;
}

you cannot rebind a reference. Once a reference refers to something, it can never be made to refer to something else. This code (if you executed it) would assign AppIfx.PlotArgs to whatever PlotArgs refered to, that's clearly not what you intended.

You must move this code into the constructor

CClassWrap::CClassWrap( CClass AppIfx ) : PlotArgs(AppIfx.PlotArgs)
{
}

But also note that this constructor code copies the CClass object, so it might not do what you expect (you might end up refering to a copied PlotArgs object, although that depends on how the CClass copy constructor works). So it's probably better to use a reference here as well

CClassWrap::CClassWrap( CClass& AppIfx ) : PlotArgs(AppIfx.PlotArgs)
{
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!