在 c++ 中有 2 个类时,如何知道使用哪个类
How to know which class is used when having 2 classes in c++?
我正在为明天早上的考试做准备。我正在努力完成以下C++演练。我已经运行了代码并使用cout来检查程序的执行。我注意到的第一件事是,该程序仅针对main中的第一个对象调用了3次类"one"中的默认构造函数。我对代码的执行感到非常困惑。
#include <iostream>
using namespace std;
class one {
int n;
int m;
public:
one() { n = 5; m = 6; cout << "one one maden"; }
one(int a, int b) {
n = a;
m = b;
cout << "made one onen";
}
friend ostream &operator<<(ostream &, one);
};
ostream &operator<<(ostream &os, one a) {
return os << a.n << '/' << a.m << '=' <<
(a.n/a.m) << 'n';
}
class two {
one x;
one y;
public:
two() { cout << "one two maden"; }
two(int a, int b, int c, int d) {
x = one(a, b);
y = one(c, d);
cout << "made one twon";
}
friend ostream &operator<<(ostream &, two);
};
ostream &operator<<(ostream &os, two a) {
return os << a.x << a.y;
}
int main() {
two t1, t2(4, 2, 8, 3);
cout << t1 << t2;
one t3(5, 10), t4;
cout << t3 << t4;
return 0;
}
我不明白第一件事。当 main 调用第一个默认构造函数时two t1,
为什么连续调用三次,然后它会调用t2(4, 2, 8, 3);
?
如果代码太长,我很抱歉,但我真的需要帮助来理解它。
请指教。谢谢。
运行代码时得到以下结果:
one one made
one one made
one two made
one one made
one one made
made one one
made one one
made one two
这是因为:
two t1;
one one made //t1.x; parameterless 'one' constructor called by default
one one made //t1.y; parameterless 'one' constructor called by default
one two made //t1; parameterless 'two' constructor
t2(4, 2, 8, 3)
one one made //t2.x; default constructor as variable not present in initialization list
one one made //t2.y; default constructor as variable not present in initialization list
made one one //x = one(a, b) executed now
made one one //y = one(c, d) executed now
made one two //t2(int..) constructer called
请注意,在 t2 的情况下,x 和 y 被构造两次,因为没有初始化列表。为避免这种情况,您可以使用:
two(int a, int b, int c, int d): x(a,b), y(c,d)
{
cout << "made one twon";
}
您将看到类"一"和类"二"的构造函数出现三次,因为创建了两个对象的三个实例。
如果您仔细查看插入器友元函数,类 1 和类 2 都是按值而不是按引用传递的。 必须通过默认复制构造函数(尚未实现)创建临时实例。 如果要取消额外的实例化,请将插入器函数更改为以下内容:
friend ostream &operator<<(ostream &, one &obj);
friend ostream &operator<<(ostream &, two &obj);
当我进一步查看时,两个有 2 个类型 1 的成员变量,所以你会看到更多的构造函数。
最后,像这样的测试类应该命名为Foo和bar。 一和二连自己都很难沟通。(恕我直言)
从 main 中的第一个对象开始。调用"one"的两个默认构造函数是因为类"two"有两个类"one"的对象,并且一个默认构造函数正常调用"Two"。
这是我看到的输出:
one one made
one one made
one two made
one one made
one one made
made one one
made one one
made one two
5/6=0
5/6=0
4/2=2
8/3=2
made one one
one one made
5/10=0
5/6=0
这对我来说非常有意义。我没有看到第一个对象调用 3 次的默认构造函数。
就输出而言,这里会发生什么:
two t1, t2(4, 2, 8, 3);
对于 t1,它为类中定义的两个对象调用 one 的默认构造函数
two (one x and one y)
所以输出是"一个制造"和"一个制造"接下来,它执行两个的默认构造函数所以输出是"一二制造"接下来对于 T2,它再次为 X 和 Y 调用默认构造函数 1所以输出是"一个制造"和"一个制造"接下来它执行
x = one(a,b) and y =one(c,d)
所以现在它打印"做了一个"和"做了一个"现在在 two() 的构造函数中,正如我们"制作了一个二"一样,同样被打印出来......
cout << t1 << t2;
one t3(5, 10), t4;
对于这个语句,对于 t3,它调用 one 的构造函数并打印"make one one"对于 t4,它执行默认构造函数并打印"一个制造"
cout << t3 << t4;
- 努力将整数转换为链表。不知道我在这里做错了什么
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 叮当不知道PTRDIFF_MAX?
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- Python中的for循环与C++有何不同
- 不知道某个东西是否被忽略会引入未定义的行为吗
- 我想知道长双倍和双倍之间的区别
- 我需要知道编译器如何在cpp中使用析构函数
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 如何知道QDataStream不能反序列化某些内容
- 有没有办法知道Tracer是否成功地完全连接到了jaegerclientcpp中的jaeger后端服务器
- 如何知道整数中的位数
- 有人知道为什么在开关中使用stoi函数会返回恒定的错误吗
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 为什么需要知道一个类是平凡的还是有平凡的构造函数
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 我需要知道如何在 c++ 中使用增量值添加到一定数字