在 c++ 中有 2 个类时,如何知道使用哪个类

How to know which class is used when having 2 classes in c++?

本文关键字:何知道 c++ 中有      更新时间:2023-10-16

我正在为明天早上的考试做准备。我正在努力完成以下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;