使用引用访问类对象C ++ [英] Using references to access class objects C++

查看:187
本文介绍了使用引用访问类对象C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一个让我陷入困境。我想做的是在包装类中获取一个引用变量,指向它包装的类中的struct对象,以便从使用包装类的其他类的结构中的变量的任何设置实际上设置在包装类不是wrappper类。为了做到这一点,我试图简单地创建一个引用在wrap类中的包裹类中的结构如

  class CClassWrap 
{
CClass :: plot_type& PlotArgs;
}

然后init PlotArgs

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

我只想让PlotArgs指向被包装的类PlotArgs,是从这个

 访问类别StudiesBase:public CClassWrap 
{
// 12应该显示在CClass,因为CClassWrap中的PlotArgs指向CClass上的PlotArgs。
PlotArgs.value = 12;

}

12显示在PlotArgs的包装类版本中。为此,我尝试如下设置在.h文件中定义的引用:

  #includeCClass.h

class CClassWrap
{
// PlotArgs是存在于CClass中的一些变量(int,long等)的结构
CClass :: plot_type& PlotArgs;
}

CClassWrap在另一个类中继承,称为StudiesBase

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

编译时,会出现一个错误,说明CClassWrap没有默认的ctor。所以我添加一个Ctor



,这样CClassWrap现在看起来像

  class CClassWrap 
{
public:
CClassWrap();
public:
// PlotArgs是一个存在于CClass中的几个变量(int,long等)的结构
CClass :: plot_type& PlotArgs;
}

这会产生一个错误C2758,说PlotArgs不是inititlaized。



因此在ClassWrap的ctor中,我尝试初始化它。

  PlotArgs = AppIfx.PlotArgs; 

其中AppIfx在运行时设置为指向CClass对象的指针。编译器不喜欢那个错误C2758变量必须initialzied在构造函数base /成员初始化列表etc ...



如果它似乎我试图做一些我完全清楚如何做的事肯定是这样的!任何帮助将不胜感激。

解决方案

这是您错误的地方

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

您不能重新绑定引用。一旦引用指向某事,它就永远不能指向别的东西。这个代码(如果你执行它)将 AppIfx.PlotArgs 分配给任何 PlotArgs 指向,这显然不是你

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

但是请注意,这个构造函数代码复制了CClass对象,所以它可能不会做你期望的(你可能最终引用一个复制的PlotArgs对象,虽然这取决于CClass复制构造函数的工作原理)。因此,最好在这里使用引用

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


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.

解决方案

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

这篇关于使用引用访问类对象C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆