Search code examples
c++syntaxconstructorpimpl-idiom

Strange "type class::method() : stuff " syntax C++


While reading some stuff on the pImpl idiom I found something like this:

MyClass::MyClass() : pimpl_( new MyClassImp() )

First: What does it mean?
Second: What is the syntax?
Sorry for being such a noob.


Solution

  • This defines the constructor for MyClass.

    The syntax is that of a constructor definition with an initialization list (I assume there is a set of braces following this that define the body of the constructor).

    The member pimpl_ of MyClass is being initialized as a pointer to a new object of type MyClassImp. It's almost the same as the following:

    MyClass::MyClass()
    {
        pimpl_ = new MyClassImp();
    }
    

    However, it is preferable to use the initialization list for initializing class members wherever possible; see the C++ FAQ Lite entry linked above.