【问题标题】:How should i return an object from a conversion function if the class of the returned object is defined later?如果稍后定义了返回对象的类,我应该如何从转换函数返回对象?
【发布时间】:2012-04-16 10:50:36
【问题描述】:
class time24;
class time12
{
operator time24()
{
...
return time24(temp) // error
}
}
class time24
{
...
};
错误 C2440: '' : 无法从 'int' 转换为 'time24'
我怎样才能返回对象来克服这个错误
【问题讨论】:
标签:
c++
type-conversion
return-type
data-conversion
【解决方案1】:
您将实现移到类定义之后的实现文件中:
//header.h
class time24;
class time12
{
operator time24();
}
class time24
{
...
};
//implementation.cpp
#include "header.h"
time12::operator time24()
{
return time24(temp) // error
}
我假设您打算实现 operator time24()。