Search code examples
c++c++11typecast-operatortypecasting-operator

Type casting operator is not being called


I was trying to do some example code for type conversions between objects, just following what is showed here.

This is the parent class

// ParentClass.h
class ParentClass {
public:
    ParentClass();
    virtual ~ParentClass();
};

It´s just some concept proof, the only thing it does is print something

#include <iostream>
#include "ParentClass.h"
ParentClass::ParentClass()
{
    std::cout << "Parent\tDefault constructor\n";
}
ParentClass::~ParentClass()
{
    std::cout << "Parent\tDestructor\n";
}

And then, the derived class

// ChildClass.h
#include "ParentClass.h"

class ChildClass: public ParentClass {
public:
    ChildClass();
    // conversion constructor
    ChildClass( const ParentClass& p );
    // conversion from assignment
    ChildClass& operator= ( const ParentClass & p );
    // type-cast operator
    operator ParentClass();
    // destructor
    virtual ~ChildClass();
};

And then, the code, is also rather stupid

// ChildClass.cpp

#include <iostream>
#include "ChildClass.h"

ChildClass::ChildClass()
{
    std::cout << "Child\tDefault constructor\n";
}
ChildClass::~ChildClass()
{
    std::cout << "Child\tDestructor\n";
}
// conversion constructor
ChildClass::ChildClass( const ParentClass& p )
{
    std::cout << "Child\tConversion constructor\n";
}
// assignment operator    
ChildClass& ChildClass::operator= ( const ParentClass & p )
{
    std::cout << "Child\toperator= conversion\n";
    return *this;
}    
// type-cast operator
ChildClass::operator ParentClass()
{
    std::cout << "Child\toperatorParentClass()\n";
    return ParentClass();
}

Then, I do some conversions in my main function

    std::cout << "calls Parent default constructor\n";
    ParentClass foo;
    std::cout << "calls Parent to child constructor\n";
    ChildClass bar = foo;
    std::cout << "calls assignment operator from child\n";
    bar = foo;
    std::cout << "calls Child type-cast operator\n";
    foo = bar;
    std::cout << "Exit\n";

But when I run this the code is not entering the type cast operator from the derived class, it just prints this:

calls Parent default constructor
Parent  Default constructor
calls Parent to child constructor
Parent  Default constructor
Child   Conversion constructor
calls assignment operator from child
Child   operator= conversion
calls Child type-cast operator
Exit
Child   Destructor
Parent  Destructor
Parent  Destructor

I am at a loss about this, as I tried the code in the link above and it worked just like it should.


Solution

  • Derived-to-base conversion never invokes a conversion function. [class.conv.fct]/p1:

    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.

    In your foo = bar;, the bar binds directly to the const ParentClass& parameter of the implicitly-declared copy assignment operator of ParentClass.