指针故障
我无法让我的指针正常工作。在我的主文件中,我在 MaxResults 类中声明
Analysis2 analysis = Analysis2();
MaxResults maxresults = MaxResults( analysis);
Now,我想指向分析,以便如果它的任何变量发生变化,我仍然可以获得正确的值。现在,我在 MaxResults 标头中将构造函数声明为
MaxResults(Analysis2 analysis);
Analysis2 * analysis2;
在 MaxResults 类中,
MaxResults(Analysis2 analysis)
{
analysis2 = analysis;
}
当我尝试从已更改的分析中访问内容时,analysis2 似乎没有跟上。我该如何解决这个问题,是否有一种简单的方法来记住指针以及将来的引用和取消引用?
I am having trouble getting my pointers to work correctly. In my main file I declare
Analysis2 analysis = Analysis2();
MaxResults maxresults = MaxResults( analysis);
Now in my MaxResults class, I want to point to analysis so that if any of its variables change I still get the right value. Right now I declare the constructor in the MaxResults header as
MaxResults(Analysis2 analysis);
Analysis2 * analysis2;
And in the MaxResults class
MaxResults(Analysis2 analysis)
{
analysis2 = analysis;
}
When I try to access things from analysis that changed, analysis2 doesn't seem to be keeping up. How do I fix this, and is there an easy way to remember pointers and referencing and dereferencing in the future?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望 MaxResults 保留指向 Analysis2 对象的指针,您应该这样做:
并按如下方式构造它:
注意地址运算符 ( &) 捕获
analysis
的地址并将其作为指针传递给MaxResults
构造函数,该构造函数将指针保存在成员中(使用初始化列表,这或多或少不太相当于做analysis = an
在构造函数的主体中,以防万一:
语法对您来说是新的)。要进一步阅读,请查看参考资料(当然,一旦您对指针有了更好的理解)。在这种情况下,参考文献可能是首选。
If you want
MaxResults
to keep a pointer to anAnalysis2
object, you should do it like this:and construct it like this:
Note the use of the address-of operator (&) to capture the address of
analysis
and pass it as pointer to theMaxResults
constructor, which saves the pointer in a member (using an initialization list, which is more or less equivalent to doinganalysis = an
in the constructor's body, just in case the:
syntax is new to you).For further reading, have a look at references (once your got a better understanding of pointers, of course). In this case, references would probably be preferred.
我不太确定这是如何编译的;不应该。问题是你的构造函数。
首先,如果您的构造函数仅采用 Analysis2 参数,则该参数是按值传递的;对象总是被复制。指针不可能指向传入的实际对象,因为您的构造函数仅获取它的副本。您需要使用引用参数:
其次,您不能直接将对象分配给指向该对象的指针。您需要使用
&
运算符来获取对象的地址。一个不相关的说明:为构造函数使用成员初始值设定项列表是一种很好的风格。
I'm not really sure how that's compiling; it shouldn't. The problem is your constructor.
First, if your constructor simply takes an
Analysis2
parameter, that parameter is pass-by-value; the object is always copied. There's no way that the pointer can point to the actual object that was passed in, since your constructor only gets a copy of it. You need to use a reference parameter:Second, you can't directly assign an object to a pointer to that object. You need to use the
&
operator to take the address of the object.On an unrelated note: it's good style to use a member initializer list for the constructor.
当您调用
MaxResults maxresults = MaxResults(analysis);
时,您的构造函数会获取analysis
的副本。您需要传入对analysis的引用,以便构造函数获取可以存储在指针中的地址。这样 maxresults 就有一个指向原始对象的指针,而不仅仅是原始对象的副本。在标题中执行类似的操作:
在类定义中执行此操作:
并以这种方式创建对象:
When you call
MaxResults maxresults = MaxResults(analysis);
, your constructor gets a copy ofanalysis
. You need to pass in the reference toanalysis
so the the constructor gets the address that it can store in a pointer. That waymaxresults
has a pointer to the original object, rather than just a copy of the original object.Do something like this in the header:
Do this in the class definition:
And create the objects this way: