示例代码:我可以像这样投射一个数组吗?
#include <cstdlib>
#include <iostream>
using namespace std;
class A {
public:
A(int x, int y) : x(x), y(y) {}
int x, y;
};
class B {
public:
operator A() {
return A(x,y);
}
float x, y;
};
void func1(A a) {
cout << "(" << a.x << "," << a.y << ")" << endl;
}
void func2(A *a, int len) {
for(int i=0; i<len; ++i) {
cout << "(" << a->x << "," << a->y << ")";
}
cout << endl;
}
int main(int argc, char** argv) {
B b[10];
func1(b[0]);
//func2(b, 10);
return(EXIT_SUCCESS);
}
func1
作品如预期,但func2
抛出一个编译时间错误。有什么我可以添加到类B
使这项工作?我怀疑不是,但问问对不对,对吗?
我认为它不会工作,因为A
的大小不同于B
的大小?
是啊我也是................. – stefanB 2009-09-07 23:41:02
我做到了。谢谢 – mpen 2009-09-07 23:41:22
所以...我不能添加'operator * A()'? – mpen 2009-09-08 05:32:45