Output Questions

Asked 2 years ago, Updated 2 years ago, 79 views

#include <stdio.h>


class myclass 
{
public:

    myclass() { printf("a"); }
    myclass(myclass& r)  { printf("b"); }
    void operator = (myclass& r){ printf("c"); } 

};

int main()
{
    myclass a;
    myclass b(a);
    myclass m = a;
    return 0;
}

I was solving the problem, but I don't know well, so I'm posting a question If you run this code, it will be printed in Abb order.

Please give me a detailed explanation of the process and when the above codes are called.

c++ class

2022-09-22 08:20

1 Answers

When you first do myclass a as you see it, the default generator myclass() is called and a is output.

The myclass b(a) then calls the myclass(myclass&r) constructor that receives the myclass object as a reference parameter and outputs b.

Finally, if you do myclass m=a, you seem to have mistakenly called the copy substitution operator, but because it is the time of generation, you call the second constructor, and b is output.


2022-09-22 08:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.