有一个包含一些数字字段 h 和 w 的类,该类还包含组合。是否有可能在每个组合的方法中访问包含它们的类的字段 h 和 w?
class circle {
int radius;
int x;
int y;
public:
circle(int x, int y, int r,){
...
}
void draw () {
/*тут каким-то образом надо использовать h и w от "носителя композиции" */
}
}
class figura1 {
int h;
int w;
circle c1;
circle c2;
circle c3;
public:
figura1(int w, int h) {
this->w=w;this->h=h;
}
void drawfigura {
с1.draw();
с2.draw();
с3.draw();
}
}
由于我有很多像 fugura1 (fugura1,fugura2,fugura3...,fuguraN)这样的课程,这一事实更加严重。它们在不同数量的组合中有所不同 circle ,除了 circle 还有其他基元:矩形、三角形。他们还需要传递链接或指向组合“载体”的指针
这里添加了这个基础。一切都编译。