Calling a class's constructor inside another class

后端 未结 3 1608
余生分开走
余生分开走 2021-01-25 01:38

I have a test next week for c++ and I\'m preparing myself for it. I\'m confused when I have 2 classes as shown below. I have to walk through the execution of the code, line by l

相关标签:
3条回答
  • 2021-01-25 02:07
    x = one(a, b);  //here is my problem
    y = one(c, d);  //here is my problem
    

    What this code does is that it calls the constructor of the class one and assigns the newly created instance of this class to the variables x and y.

    The constructor of class one is in line 9.

    0 讨论(0)
  • 2021-01-25 02:08

    from the line x = one(a, b); it jumps to line one(int a, int b) and executes the parameterized constructor of one

    same for line y = one(c, d);

    0 讨论(0)
  • 2021-01-25 02:24

    Current approach works only if you have a default constructor in one class. It is better to initialize members in constructor initialization list:

    two(int a, int b, int c, int d) 
        : x(a,b), y(c,d)
    {
            cout << "made one two\n";
    }
    
    0 讨论(0)
提交回复
热议问题