C++ Define Conversion to Base Class


I'm having difficulty defining and using a conversion operator to a base class. Consider the following:

class base { public: base(const base&); base& operator=(const base&); //some stuff }; class child : public base { public: operator base() const; //some more stuff }; int main() { child c; base b=c; b=c; }

If I attempt to convert a child to a base, operator base() is never called (even if I make the conversion explicit). Instead, the copy constructor or assignment operator from the base class is called directly, without a conversion.

How can I cause operator base() to be called when a child is assigned (or copy-constructed) to a base?


A conversion function to a base class is syntactically legal, but it will never be called. From standard draft n3337:


<strong>12.3.2 Conversion functions [class.conv.fct] §1</strong>

[...] A conversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it), to a (possibly cv-qualified) base class of that type (or a reference to it), or to (possibly cv-qualified) void. [...]


The language has got different mechanism for conversions from derived to base. Read about <a href="https://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c" rel="nofollow">object slicing</a>. To put it simply - it's done automatically for you.


  • Is this Prolog terminology correct?
  • Can a class be nullified from within the class itself?
  • Why does a type conversion not work in Java [duplicate]
  • Declaring pointers to function returning arrays is actually legal?
  • Thinning down a jquery focus script?
  • Java - Securely store user account details required for use in code
  • Choosing Correct papersize when printing with .NET PrintDocument
  • extract a double or integer from a string using regular expression in java?
  • Legal legacy code using pointers suddenly becomes UB
  • DirectX game with no prerequisite software to run
  • C Language: Why does malloc() return a pointer, and not the value?
  • What version of Java should I use with Cassandra 2.0?
  • Efficient User-Agent Regex to find Safari in Python
  • Most efficient way to move table rows from one table to another
  • MySQL performance when updating row with FK
  • How to explicitly/implicitly implemented interface members in C++/CLI?
  • Convert SQLite database to XML
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • Conversion from string “a” to type 'Boolean' is not valid
  • uniform generation of points on 3D box
  • How can I set a binding to a Combox in a UserControl?
  • Create DicomImage from scratch using Dcmtk
  • Functions in global context
  • Typescript - Unable to get 'import' statement to function
  • Running a C# exe file
  • Why winpcap requires both .lib and .dll to run?
  • XCode can't find symbols for a specific iOS library/framework project
  • Arrays break string types in Julia
  • python regex in pyparsing
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Running Map reduces the dimensions of the matrices
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?