I've got access violation error after returning Foo1 object with pointer to Foo class. Result of function Foo2::something(int)
is Foo1 object . The problem is : Foo** array
of Foo2::something(int)
result is inaccessible.
class Foo{
int x;
public:
int getX() { return x; }
Foo& operator=(const Foo &rhs){
x = rhs.x;
return *this;
}
};
class Foo1{
Foo** array;
int size;
public:
Foo1(int size){
array = new Foo*[size]
for(int i=0; i < size;i++)
array[i] = new Foo[size];
}
Foo1(const Foo1& foo): array(foo.array), size(foo.size){}
~Foo1(){
for(int i=0; i < size);i++)
delete[] array[i];
delete[] array;
}
Foo getFoo(int x, int y){
return array[x][y];
}
void setFoo(int x,int y,Foo foo){
array[x][y] = foo;
}
Foo1& operator=(const Foo1& foo){
array = foo.array;
size = foo.size;
}
};
class Foo2{
public:
Foo1 something(int size){
Foo1 obj(size);
return obj;
}
};
int main(){
Foo2 foo2;
Foo1 obj = foo2.something(3);
obj.getFoo(0,0).getX(); // <- access violation here
}