小编fra*_*ben的帖子

关于通过引用传递的怀疑

我正在学习C++,特别是我已停止参考.如果我的问题对你们大多数人来说都是微不足道的,我很抱歉,但我想了解这个程序的输出:

#include <iostream>

using namespace std;

struct myStruct
{
    int a;
    int b;
};
typedef struct myStruct myStruct;

myStruct copyMyStruct(myStruct& source)
{
    myStruct dest;
    dest.a=source.a;
    dest.b=source.b;
    return dest;
}

myStruct otherCopyMyStruct(myStruct& source)
{
    myStruct dest;
    dest=source;
    return dest;
}

myStruct& GetRef(myStruct& source)
{
    return source;
}

void printMyStruct(string name,const myStruct& str)
{
    cout<<name<<".a:"<<str.a<<endl;
    cout<<name<<".b:"<<str.b<<endl;
}

myStruct one,two,three,four;
myStruct& five=one;

void printStructs()
{
    printMyStruct("one",one);
    printMyStruct("two",two);
    printMyStruct("three",three);
    printMyStruct("four",four);
    printMyStruct("five",five);
}

int main()
{
    one.a=100;
    one.b=200;

    two=copyMyStruct(one);
    three=otherCopyMyStruct(one);
    four=GetRef(one);


    printStructs();

    cout<<endl<<"NOW MODIFYING one"<<endl; …
Run Code Online (Sandbox Code Playgroud)

c++ reference function

2
推荐指数
1
解决办法
205
查看次数

标签 统计

c++ ×1

function ×1

reference ×1