未调用类型转换运算符
Type casting operator is not being called
我正在尝试为对象之间的类型转换做一些示例代码,只是按照显示的内容 here。
这是父级 class
// ParentClass.h
class ParentClass {
public:
ParentClass();
virtual ~ParentClass();
};
这只是一些概念证明,它唯一做的就是打印一些东西
#include <iostream>
#include "ParentClass.h"
ParentClass::ParentClass()
{
std::cout << "Parent\tDefault constructor\n";
}
ParentClass::~ParentClass()
{
std::cout << "Parent\tDestructor\n";
}
然后,派生的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();
};
然后,代码,也比较笨
// 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();
}
然后,我在我的主函数中做一些转换
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";
但是当我 运行 这个代码没有从派生的 class 输入类型转换运算符时,它只是打印这个:
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
我对此一头雾水,因为我尝试了上面 link 中的代码,它运行正常。
派生到基础的转换从不调用转换函数。 [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
.
在您的 foo = bar;
中,bar
直接绑定到 ParentClass
的隐式声明的复制赋值运算符的 const ParentClass&
参数。
我正在尝试为对象之间的类型转换做一些示例代码,只是按照显示的内容 here。
这是父级 class
// ParentClass.h
class ParentClass {
public:
ParentClass();
virtual ~ParentClass();
};
这只是一些概念证明,它唯一做的就是打印一些东西
#include <iostream>
#include "ParentClass.h"
ParentClass::ParentClass()
{
std::cout << "Parent\tDefault constructor\n";
}
ParentClass::~ParentClass()
{
std::cout << "Parent\tDestructor\n";
}
然后,派生的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();
};
然后,代码,也比较笨
// 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();
}
然后,我在我的主函数中做一些转换
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";
但是当我 运行 这个代码没有从派生的 class 输入类型转换运算符时,它只是打印这个:
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
我对此一头雾水,因为我尝试了上面 link 中的代码,它运行正常。
派生到基础的转换从不调用转换函数。 [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
.
在您的 foo = bar;
中,bar
直接绑定到 ParentClass
的隐式声明的复制赋值运算符的 const ParentClass&
参数。