-1

Below is a wrapper class I found in the stackoverflow.

class int_ptr_wrapper
{
public:
    int_ptr_wrapper(int value = 0) :
    mInt(new int(value))
    {}

    // note! needs copy-constructor and copy-assignment operator!

    ~int_ptr_wrapper()
    {
        delete mInt;
    }

private:
    int* mInt;
};

I could not understand the meaning of declaration:

    int_ptr_wrapper(int value = 0) :
    mInt(new int(value))
    {}

Can you explain the meaning of this declaration in details?

Sunny Lei
  • 181
  • 1
  • 3
  • 15

1 Answers1

1

Constructor uses initialization list in which you simply dynamically allocate memory for mInt variable.

That constructor is the same as this:

int_ptr_wrapper(int value = 0){
mInt = new int(value);
}
wdc
  • 2,623
  • 1
  • 28
  • 41