谁能解释一下上面的代码是如何工作的以及使用哪个概念
Can anyone explain me how above code works and using which concept?
#include<iostream>
using namespace std;
class A
{
public:
A(int x)
{
a=x;
}
int a;
};
void fun(A temp)
{
cout<<temp.a<<endl;
}
int main()
{
fun(1);
}
在这里,我们将原始值传递给有趣的方法并使用 A类的对象。 谁能解释一下上面的代码是如何工作的以及使用哪个概念?
我为您注释了代码,并使其阅读起来更清晰一些。阅读 (1( -> (5( 中的评论。
#include <iostream>
using namespace std;
class A {
public:
A(int x) // (3): gets called by f to initialize an object of class A with x=1
{
a = x;
}
int a;
};
void fun(A temp) // (2): Assumes 1 is a type class A. But it hasn't yet been initialized. So it calls Constructor. (tmp = new A(1))
{
// (4): Now temp is an object of class A
cout << temp.a << endl; // Prints temp.a
// (5): temp object is getting destroyed.
}
int main()
{
fun(1); // (1): Calls fun() with argument of 1
}
类 A 有一个构造函数,它接收一个整数作为参数。
因此,传递给 fun 的整数参数会自动转换为 A。
在此自动转换过程中,将创建一个 A 对象(称为 temp(,其字段"a"初始化为 1。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 有人能分解一下这个c++模板的语法吗
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 有人能解释一下为什么下界是这样工作的吗C++的
- 有人可以解释一下这个矩阵幂函数是如何工作的吗?
- 解释一下这个排序算法是如何工作的?
- 谁能解释一下这个BFS代码是如何工作的?
- 解释一下这个宏是如何工作的?[C 中的打印var名称]
- 谁能解释一下上面的代码是如何工作的以及使用哪个概念
- 有人可以向我解释一下这段代码是如何工作的吗?(程序在预期数值且用户输入其他输入时再次要求用户输入)
- 有人能解释一下这个功能是如何工作的吗
- 有人可以解释一下工会在这一行代码中是如何工作的,以及数字是如何交换的
- 有人能简要地解释一下set_union在c++中是如何工作的吗