c++ - How to initialize a dynamic array of pointers to an object? -
class leg { public: leg (const char* const s , const char* const e , const double d) : startcity (s), endcity (e), distance (d) {} friend void outputleg( ostream& , const leg& ) ; private: const char* const startcity ; const char* const endcity ; const double distance; }; class route { public: /* include 2 public constructors -- (1) 1 create simple route consisting of 1 leg, first constructor's parameter should const reference leg object.*/ route ( const leg& ) : arrayholder( new const leg* [1] ), arraysize( 1 ), r_distance( leg.distance ) {} //(2) create new route adding leg end of existing route. private: const leg** const arrayholder;//save dynamically-sized array of leg*s const leg** const. const int arraysize;//save size of leg* array const int . const double r_distance;//store distance of route const double, computed sum of distances of legs. };
i use clarification on doing in first constructor. how correctly save pointer of passed leg object?
currently getting 'in constructor 'route::route(const leg&)': error: expected primary-expression before '.' token'
your error when trying access leg.distance. if distance static member of leg, need access leg::distance. want create one-leg route, seems distance member variable, , need specify parameter name in function definition:
route (const leg& l) : arrayholder( new const leg* (&l)), arraysize(1), r_distance(l.distance) {}
Comments
Post a Comment