C ++类Shape没有名为set_values的成员

时间:2014-11-17 19:54:17

标签: c++ arrays inheritance

Classes Triangle,Rectangle继承形状。我想创建一个包含Shape类型元素的数组,可以是Triangle或Rectangle。我写道:

Shape** foo = new Shape*[2];
foo[0] = new Triangle();
foo[0].set_values(3,5) // set_values function exist only in Triangle class

这给了我class Shape has no member named set_values。如何调用set_values函数?

1 个答案:

答案 0 :(得分:1)

Shape** foo = new Shape*[2];
foo[0] = new Triangle();
// foo[0].set_values(3,5) // set_values function exist only in Triangle class
Triangle* tri = static_cast<Triangle*>(foo[0]);
tri->set_values(3,5);