澄清需要在COUT中的功能调用

发布于 2025-01-25 07:35:27 字数 807 浏览 3 评论 0原文

我正在与测验一起在线C ++学习课程。该片段的最后一个输出行要确定(我添加了我的评论)。正确答案:10。我的问题:为什么10而不是11? 调用A(b)交换两个变量,那么为什么最后一个AAB 0而不是1个变量? /为什么COUT中的AB()不影响AAB?

#include <iostream>
using namespace std;

class classA {
public:
    classA() { st.f = st.p = 1; }
    struct { int f, p; } st;
    int bfunc(void);
};

int classA::bfunc(void) { int x = st.f; st.f = st.p; st.p = x; return x; };

int main()
{
    classA a;
    a.st.f = 0;
    cout << a.st.f << a.st.p << endl; //01
    a.bfunc();
    cout << a.st.f << a.st.p << endl;  //10
    a.bfunc();
    cout << a.st.f << a.st.p << endl; //01
    a.bfunc();
    cout << a.st.f << a.st.p << endl; //10
    cout << a.bfunc() << a.st.p << endl; //10
    return 0;
}

i'm doing an online c++ learning course with quiz. The last output line of this snippet is to be determined (comments added by me). Correct answer: 10. My question: why 10 and not 11?
Calling a(b) swaps the two variables, so why is the last a.a.b 0 and not 1? / Why does the a.b() in cout not affect the a.a.b?

#include <iostream>
using namespace std;

class classA {
public:
    classA() { st.f = st.p = 1; }
    struct { int f, p; } st;
    int bfunc(void);
};

int classA::bfunc(void) { int x = st.f; st.f = st.p; st.p = x; return x; };

int main()
{
    classA a;
    a.st.f = 0;
    cout << a.st.f << a.st.p << endl; //01
    a.bfunc();
    cout << a.st.f << a.st.p << endl;  //10
    a.bfunc();
    cout << a.st.f << a.st.p << endl; //01
    a.bfunc();
    cout << a.st.f << a.st.p << endl; //10
    cout << a.bfunc() << a.st.p << endl; //10
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文