constructor without initializer list having objects with deleted constructors

by simplename   Last Updated August 13, 2019 22:26 PM

I used to have my CameraMill class check if the size of the input params vector is correct and then initialize my cameras m_cam0 and m_cam1

CameraMill::CameraMill(std::vector<camera::Params> params)
{
    if (params.size() == 2)
    {
        m_cam0 = Camera(params[0]);
        m_cam1 = Camera(params[1]);
    }
}

A recent change to the Camera class has deleted the constructor. So this fails with error: use of deleted function. I could initialize them in the initializer list but then I can't check the size...

CameraMill(std::vector<Params> params)
    : m_cam0(params[0])
    , m_cam1(params[1])
{
    // if (params.size() == 2)
    // {
    //  m_cam0 = Camera(params[0]);
    //  m_cam1 = Camera(params[1]);
    // }
}

How do I go about without using initializer list?

Tags : c++


Answers 2


You can use a helper function to get the right Camera object.

CameraMill(std::vector<Params> params) : 
m_cam0(CameraMill::getParam(params, 0),
m_cam1(CameraMill::getParam(params, 1),
{
}

where

Param const& CameraMill::getParam(std::vector<Params> const& params, size_t i)
{
   (i < params.size()) : params[i] : Params();
}
R Sahu
R Sahu
August 13, 2019 22:25 PM

I could initialize them in the initializer list but then I can't check the size...

A workaround would be to call a function in the initializer list to perform the check and initialize both.

However, it would be simpler to simply call .at() instead, and then inside the body of the constructor check the size.

Acorn
Acorn
August 13, 2019 22:25 PM

Related Questions