g++ error: conversion from XTestObjectA to non-scalar type XTestObjectBase requested

//error
XTestObjectBase objTestObjectBase1 = objTestObjectA;

//ok
XTestObjectBase objTestObjectBase2(objTestObjectA);

//ok
XTestObjectBase objTestObjectBase3; objTestObjectBase3 = objTestObjectA;

cite from: http://stackoverflow.com/questions/6120240/why-constructor-is-not-called-for-given-casting-operator

The problem is that the number of user-defined conversions that are invoked implicitly is limited (to 1) by the Standard.

B ob = a;
implies two user conversions:

on a: Wrap::operator A*() should be called
on the result: B::B(A*) should be called

The solution is to use explicit conversion

//now ok
XTestObjectBase objTestObjectBase1 = XTestObjectBase(objTestObjectA);

Leave a Reply

Your email address will not be published. Required fields are marked *