c++ - Use the parent class's function with the same name and aruments -
i have following classes:
class cpolygon { spolygon* data; // populated constructor // ... members here ... public: cpolygon(); cpolygon(int type); spolygon getdata(int index); // ... methods here ... }; class csquare : public cpolygon { // ... members here ... public: csquare(); ssquare getdata(int index); // ... methods here ... }; the second cpolygon() constructor accepts type of polygon want.
is there way call (second) constructor on csquare() constructor passing value square type, like:
csquare::csquare() { cpolygon(type_square); } to make data square?
edit
i added new member , method/function. structure (and union):
union spolygon { shead head; ssquare square; // ... more structure here ... } struct ssquare { shead head; // ... more membere here ... } i want return square data:
ssquare csquare::getdata(int index) { return getdata(index).square; } but getdata() (inside function) considered csquare's need cpolygon's. how?
you can use member initialization list:
csquare::csquare(): cpolygon(type_square) {} this invokes cpolygon's constructor first, according correct type, csquare() constructor body executed.
note: initialization lists apply not super-classes, ordinary class members. examples, please refer c++ member initialization list.
for edited question, use cpolygon::getdata call specified version of getdata:
ssquare csquare::getdata(int index) { return cpolygon::getdata(index).square; }
Comments
Post a Comment