【发布时间】:2011-05-25 06:10:06
【问题描述】:
struct A {};
struct B
{
B (A* pA) {}
B& operator = (A* pA) { return *this; }
};
template<typename T>
struct Wrap
{
T *x;
operator T* () { return x; }
};
int main ()
{
Wrap<A> a;
B oB = a; // error: conversion from ‘Wrap<A>’ to non-scalar type ‘B’ requested
oB = a; // ok
}
当oB 被构造时,为什么B::B(A*) 没有被Wrap<T>::operator T () 调用? [注意:B::operator = (A*) 在下一条语句中为Wrap<T>::operator T () 调用]
【问题讨论】:
-
哪个编译器?在 VS2010 上编译良好。
-
@Agnel Kurian,那是因为 VS2010 部分支持 C++0x。
-
@Kirill:那么,以上在 C++0x 中是否有效?你是这个意思吗?
-
@Agnel Kurian,我刚刚看过 C++0x,不,它是无效的。似乎是VS2010中的扩展或错误。
-
使用 gcc 4.4.3 我无法编译
B oB;,我必须使用B oB(a);。所以我不清楚main的第三行中的// ok指的是什么。
标签: c++ constructor compiler-errors typecast-operator