指针故障

发布于 2024-11-14 11:11:54 字数 504 浏览 3 评论 0原文

我无法让我的指针正常工作。在我的主文件中,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必你懂 2024-11-21 11:11:54

如果您希望 MaxResults 保留指向 Analysis2 对象的指针,您应该这样做:

class MaxResults {

 public:
     MaxResults(Analysis* an) : analysis(an) {}

 private:
     Analysis* analysis;
};

并按如下方式构造它:

Analysis2 analysis = Analysis2();
MaxResults maxresults = MaxResults( &analysis);

注意地址运算符 ( &) 捕获 analysis 的地址并将其作为指针传递给 MaxResults 构造函数,该构造函数将指针保存在成员中(使用初始化列表,这或多或少不太相当于做analysis = an在构造函数的主体中,以防万一 : 语法对您来说是新的)。

要进一步阅读,请查看参考资料(当然,一旦您对指针有了更好的理解)。在这种情况下,参考文献可能是首选。

If you want MaxResults to keep a pointer to an Analysis2 object, you should do it like this:

class MaxResults {

 public:
     MaxResults(Analysis* an) : analysis(an) {}

 private:
     Analysis* analysis;
};

and construct it like this:

Analysis2 analysis = Analysis2();
MaxResults maxresults = MaxResults( &analysis);

Note the use of the address-of operator (&) to capture the address of analysis and pass it as pointer to the MaxResults constructor, which saves the pointer in a member (using an initialization list, which is more or less equivalent to doing analysis = 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.

真心难拥有 2024-11-21 11:11:54

我不太确定这是如何编译的;不应该。问题是你的构造函数。

首先,如果您的构造函数仅采用 Analysis2 参数,则该参数是按值传递的;对象总是被复制。指针不可能指向传入的实际对象,因为您的构造函数仅获取它的副本。您需要使用引用参数:

MaxResults(Analysis2& analysis);

其次,您不能直接将对象分配给指向该对象的指针。您需要使用 & 运算符来获取对象的地址。

MaxResults(Analysis2& analysis)
{
    analysis2 = &analysis;
}

一个不相关的说明:为构造函数使用成员初始值设定项列表是一种很好的风格。

MaxResults(Analysis2& analysis)
    : analysis2(&analysis)
{
}

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:

MaxResults(Analysis2& analysis);

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.

MaxResults(Analysis2& analysis)
{
    analysis2 = &analysis;
}

On an unrelated note: it's good style to use a member initializer list for the constructor.

MaxResults(Analysis2& analysis)
    : analysis2(&analysis)
{
}
猥琐帝 2024-11-21 11:11:54

当您调用 MaxResults maxresults = MaxResults(analysis); 时,您的构造函数会获取 analysis 的副本。您需要传入对analysis的引用,以便构造函数获取可以存储在指针中的地址。这样 maxresults 就有一个指向原始对象的指针,而不仅仅是原始对象的副本。

在标题中执行类似的操作:

MaxResults(Analysis2 *analysis);
Analysis2* analysis2;

在类定义中执行此操作:

MaxResults(Analysis2 *analysis)
{
    analysis2 = analysis;
}

并以这种方式创建对象:

Analysis2 analysis = Analysis2();
MaxResults maxresults = MaxResults(&analysis);

When you call MaxResults maxresults = MaxResults(analysis);, your constructor gets a copy of analysis. You need to pass in the reference to analysis so the the constructor gets the address that it can store in a pointer. That way maxresults has a pointer to the original object, rather than just a copy of the original object.

Do something like this in the header:

MaxResults(Analysis2 *analysis);
Analysis2* analysis2;

Do this in the class definition:

MaxResults(Analysis2 *analysis)
{
    analysis2 = analysis;
}

And create the objects this way:

Analysis2 analysis = Analysis2();
MaxResults maxresults = MaxResults(&analysis);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文