示例代碼:我可以像這樣投射一個數組嗎?
#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