Как правильно наследовать родительский класс? (no viable conversion)
Вот пример из Typecript:
class Vector2 {
x:number = 0;
y:number = 0;
}
class Cell extends Vector2{
}
const cell:Cell = new Vector2();
Но С++ мне выдаёт ошибку error: no viable conversion from 'Point' to 'Cell'
class Point {
public:
float x;
float y;
};
class Cell : public Point {
public:
Cell();
Cell(int x, int y);
};
Cell pos = Point{1,2};
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
class Point {
public:
float x;
float y;
};
class Cell : public Point {
public:
Cell();
Cell(int x, int y);
Cell(const Point& p){ x = p.x; y = p.y; }
};
Cell pos = Point{1,2};
или даже проще:
Cell(const Point& p):Point{p}{}
Варианты без конструктора:
....
Cell(int x, int y);
};
Cell pos = Cell{1,2};
....
Cell(int x, int y);
};
Cell pos{1,2};