I'm using GCC7 on Qt 5.9.4 on openSUSE Leap 15.
I have the following class:
class ManSuppProps : public QObject
{
Q_OBJECT
public:
explicit ManSuppProps(QString parentName);
explicit ManSuppProps(){}
explicit ManSuppProps(const ManSuppProps &manSuppProps);
explicit ManSuppProps(ManSuppProps &manSuppProps);
~ManSuppProps();
private:
QVector3D m_suppPos;
QString m_suppParentName;
}
With the following implementations for constructors:
ManSuppProps::ManSuppProps(QString parentName)
: QObject()
, m_suppPos(QVector3D(0, 0, 0))
, m_suppParentName(parentName)
{
qDebug()<<"Constructing ManSuppProps object ...";
}
ManSuppProps::ManSuppProps(const ManSuppProps &manSuppProps)
: QObject()
, m_suppPos(manSuppProps.getSuppPos())
, m_suppParentName(manSuppProps.getSuppParentName())
{
}
ManSuppProps::ManSuppProps(ManSuppProps &manSuppProps)
: QObject()
, m_suppPos(manSuppProps.getSuppPos())
, m_suppParentName(manSuppProps.getSuppParentName())
{
}
ManSuppProps::~ManSuppProps(){}
I'm receiving the following error:
error: no matching function for call to ‘ManSuppProps::ManSuppProps(ManSuppProps&)’
At a method of another class which has a member of class ManSuppProps
:
ManSuppProps EditorScene::manSuppProps()
{
return m_manSuppProps; // error is thrown here
}
Considering I have all the constructors, I don't get why the error is received. Can anybody help.
This is expected behavior. Note that the appropriate constructor is declared as explicit
as
explicit ManSuppProps(ManSuppProps &manSuppProps);
And return m_manSuppProps;
performs copy initialization,
4) when returning from a function that returns by value
And copy-initialization doesn't consider explicit
constructors.
(emphasis mine)
If
T
is a class type and the cv-unqualified version of the type ofother
isT
or a class derived fromT
, the non-explicit constructors ofT
are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.Copy-initialization is less permissive than direct-initialization: explicit constructors are not converting constructors and are not considered for copy-initialization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With