在另一个类构造函数中调用一个对象的构造函数

时间:2021-01-30 02:50:45

标签: c++ constructor

我需要在 C++ 中有以下结构的类。

class A
{
   int y;
   A(int x)
   {
      //Computes y as per x. 
   }
};

class B
{
    A obj;
    int w;

    B(int z)
    {
         //Need to call constructor of obj with input x = z
         //Need to set the value of w based on value of A.y
         //How to do it?
    }

};

我在互联网上找不到如何执行此操作。 obj 的构造何时会被调用,以及如何使用输入 z 调用它?

0 个答案:

没有答案