我可以像这样投射一个阵列吗?

mpe*_*pen 2 c++ casting

示例代码:

#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);
}
Run Code Online (Sandbox Code Playgroud)

func1按预期工作,但func2抛出编译时错误.有什么我可以添加到课堂B上使这项工作?我怀疑没有,但是问起来并没有伤害,对吧?

我认为它不会起作用,因为它的大小A不同于B

Ara*_*raK 5

void func2(A *a, int len)
Run Code Online (Sandbox Code Playgroud)

当您尝试通过类型的指针Bfunc2没有来自可接受的转化率B*A*.这些是由两种不同类型AB,尽管类型B具有转换操作员键入A.