Using references to access class objects C++

两盒软妹~` 提交于 2019-12-31 05:45:09

问题


This one has me stumped. What I'm trying to do is get a reference variable in a wrapper class to point to a struct object in the class it wraps so that any setting of variables in the struct from other classes that use the wrapper class, actually are set in the wrapped class not the wrappper class. To do this I tried to simply create a reference in the wrap class to the struct in the wrapped class like

class CClassWrap
{
    CClass::plot_type& PlotArgs;  
}  

and then init PlotArgs

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

I just want PlotArgs to point to the wrapped class' PlotArgs so that when PlotArgs is accessed from say this

class StudiesBase:public CClassWrap
{
   //12 should show up in CClass because PlotArgs in CClassWrap points to the PlotArgs on CClass.
   PlotArgs.value = 12;

 }

12 shows up in the wrapped classes version of PlotArgs. To do this I tried setting a reference defined in the .h file as follows

#include "CClass.h"

class CClassWrap
{
 //PlotArgs is a struct with a few vars in it (int, long, etc.) that exists in CClass
 CClass::plot_type& PlotArgs;  
}

CClassWrap is inherited in another class, call it StudiesBase

class StudiesBase:: public CClassWrap
{
     etc...
}

When this is compiled, an error is given saying no default ctor exists for CClassWrap. So I add a Ctor

such that CClassWrap now looks like

class CClassWrap
{
public:
      CClassWrap();
public:
     //PlotArgs is a struct with a few vars in it (int, long, etc.) that exists in CClass
     CClass::plot_type& PlotArgs;  
}

This generates an error C2758 saying that PlotArgs is not inititlaized.

So in the ctor for ClassWrap I try to init it.

PlotArgs = AppIfx.PlotArgs;

where AppIfx is set at runtime as a pointer to the CClass object. The compiler doesn't like that either with error C2758 variable must be initialzied in constructor base/member initializer list etc...

If it appears that I'm trying to do something that I'm totally clear on how to do that would definitely be the case! Any help would be much appreciated.


回答1:


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)
{
}


来源:https://stackoverflow.com/questions/20052288/using-references-to-access-class-objects-c

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