Search code examples
c++constructor

Why does C++ class show 2 constructors after I declared only one constructor?


I have the following C++ class:

class MyClass
{
private:
    PCWSTR _windowName{};
    PCWSTR _windowTitle{};

public:
    MyClass(PCWSTR wndName, PCWSTR wndTitle)
    {
        this->_windowName = wndName;
        this->_windowTitle = wndTitle;
    }
};

When I try to create a variable of this class type, it shows 2 constructors and one of them was not declared by me.

Constructor declared by me: enter image description here

And this one?

enter image description here

What is this second constructor?


Solution

  • Every class that doesn't have a copy constructor declared by the user has one implicitly declared. What you are seeing there is that copy constructor. It will also usually, and in your case as well, be implicitly-defined, so that it copies every member individually, if that is possible.

    The IDE is however a bit inconsistent. There should also be an implicitly-declared move constructor for your class with the signature MyClass(MyClass&&), which is also usually, and in your case as well, implicitly-declared and -defined to move each member individually, if possible, but with some additional conditions applying (e.g. no user-declared destructor). So in total the class has three constructors.