从函数返回对结构的引用
Returning a reference to a structure from a function
您好,我正在阅读有关 C++ 中的引用变量的内容,并且正在阅读有关对结构的引用以及 return 从函数中引用对结构的内容。我阅读的示例代码如下:
#include <iostream>
using namespace std;
struct sysop {
char name[26];
char quote[64];
int used;
};
const sysop & use(sysop & sysopref);
int main()
{
sysop looper = {"Rick Looper", "I'm a goto kind of guy.", 0};
use (looper);
cout << “Looper: “ << looper.used << “ use(s)\n”;
sysop copycat;
copycat = use(looper);
cout << “Looper: “ << looper.used << “ use(s)\n”;
cout << “Copycat: “ << copycat.used << “ use(s)\n”;
cout << “use(looper): “ << use(looper).used << “ use(s)\n”;
return 0;
}
const sysop & use(sysop & sysopref)
{
cout << sysopref.name << “ says:\n”;
cout << sysopref.quote << endl;
sysopref.used++;
return sysopref;
}
这是来自 C++ Primer Plus 书。不管怎样,我理解了这个例子,但让我困惑的是use(looper)
。我的意思是函数原型说 return 对常量结构变量的引用,但在这个语句中,函数没有 returning 任何引用。我不确定是否有关于引用常量结构或允许在没有 returning 数据的情况下使用函数的东西或什么的特殊之处。
有人可以解释一下吗??
你说:
but in this statement, the function is not returning any reference.
这是不正确的。该函数仍然 return 是 const
参考。 return 值在调用点被忽略。
您可以使用:
sysop const& ref = use (looper);
捕获函数的 return 值。
the statement that confused me is use(looper). I mean the function prototype says to return a reference to a constant structure variable, but in this statement, the function is not returning any reference.
是的,确实如此。您必须阅读 whole 语句,尤其是它说 return 类型是 sysop const&
的地方。那是引用类型。
在这种情况下,您将引用传递给函数,然后将相同的引用直接传回。
I mean the function prototype says to return a reference to a constant
structure variable, but in this statement, the function is not
returning any reference.
sysopref
当您接受它作为使用中的参数时,它已经是一个引用 use(sysop & sysopref)
。当你 return 来自函数的变量时,你只需简单地改变它。
const sysop & use(sysop & sysopref)
use
接受对类型 sysop
实例的非常量引用——这意味着,当我们这样调用 use
时:
sysop s;
use(s);
use
直接在 s
上工作,而不是它的副本(这就是引用的工作方式)。这是必需的,因为 use
修改了它的参数:
sysopref.used++;
然而,它 returns 它的参数作为 const 引用(可能是为了避免进一步修改或其他)。没关系,该参数是非常量引用 - 它是 "promoted"(但它不会反过来工作)。
所以,在这一系列调用之后:
sysop s;
const sysop& ret_ref = use(s);
ret_ref
将是对 s
的引用(它们将共享内存并包含完全相同的数据)。看看这个样本:
int main()
{
sysop s;
s.used = 0;
const sysop& ret_ref = use(s);
std::cout<<s.used<<std::endl;
std::cout<<ret_ref.used<<std::endl<<std::endl;
std::cout<<&(s)<<std::endl;
std::cout<<&(ret_ref)<<std::endl;
return 0;
}
输出:
1
1
0xbfc910f0
0xbfc910f0
可以看到,它们的内存地址完全一样,所以函数确实返回了传参。
试试这个代码 here。
您好,我正在阅读有关 C++ 中的引用变量的内容,并且正在阅读有关对结构的引用以及 return 从函数中引用对结构的内容。我阅读的示例代码如下:
#include <iostream>
using namespace std;
struct sysop {
char name[26];
char quote[64];
int used;
};
const sysop & use(sysop & sysopref);
int main()
{
sysop looper = {"Rick Looper", "I'm a goto kind of guy.", 0};
use (looper);
cout << “Looper: “ << looper.used << “ use(s)\n”;
sysop copycat;
copycat = use(looper);
cout << “Looper: “ << looper.used << “ use(s)\n”;
cout << “Copycat: “ << copycat.used << “ use(s)\n”;
cout << “use(looper): “ << use(looper).used << “ use(s)\n”;
return 0;
}
const sysop & use(sysop & sysopref)
{
cout << sysopref.name << “ says:\n”;
cout << sysopref.quote << endl;
sysopref.used++;
return sysopref;
}
这是来自 C++ Primer Plus 书。不管怎样,我理解了这个例子,但让我困惑的是use(looper)
。我的意思是函数原型说 return 对常量结构变量的引用,但在这个语句中,函数没有 returning 任何引用。我不确定是否有关于引用常量结构或允许在没有 returning 数据的情况下使用函数的东西或什么的特殊之处。
有人可以解释一下吗??
你说:
but in this statement, the function is not returning any reference.
这是不正确的。该函数仍然 return 是 const
参考。 return 值在调用点被忽略。
您可以使用:
sysop const& ref = use (looper);
捕获函数的 return 值。
the statement that confused me is use(looper). I mean the function prototype says to return a reference to a constant structure variable, but in this statement, the function is not returning any reference.
是的,确实如此。您必须阅读 whole 语句,尤其是它说 return 类型是 sysop const&
的地方。那是引用类型。
在这种情况下,您将引用传递给函数,然后将相同的引用直接传回。
I mean the function prototype says to return a reference to a constant structure variable, but in this statement, the function is not returning any reference.
sysopref
当您接受它作为使用中的参数时,它已经是一个引用 use(sysop & sysopref)
。当你 return 来自函数的变量时,你只需简单地改变它。
const sysop & use(sysop & sysopref)
use
接受对类型 sysop
实例的非常量引用——这意味着,当我们这样调用 use
时:
sysop s;
use(s);
use
直接在 s
上工作,而不是它的副本(这就是引用的工作方式)。这是必需的,因为 use
修改了它的参数:
sysopref.used++;
然而,它 returns 它的参数作为 const 引用(可能是为了避免进一步修改或其他)。没关系,该参数是非常量引用 - 它是 "promoted"(但它不会反过来工作)。
所以,在这一系列调用之后:
sysop s;
const sysop& ret_ref = use(s);
ret_ref
将是对 s
的引用(它们将共享内存并包含完全相同的数据)。看看这个样本:
int main()
{
sysop s;
s.used = 0;
const sysop& ret_ref = use(s);
std::cout<<s.used<<std::endl;
std::cout<<ret_ref.used<<std::endl<<std::endl;
std::cout<<&(s)<<std::endl;
std::cout<<&(ret_ref)<<std::endl;
return 0;
}
输出:
1
1
0xbfc910f0
0xbfc910f0
可以看到,它们的内存地址完全一样,所以函数确实返回了传参。
试试这个代码 here。