oop - Return a string as private inherited class in c++ -
i have class privately inherits std::string
. want have member function returns string
base class. how do it?
class newclass() : private std::string { ... public: std::string getstring() const; ... }; std::string newclass::getstring() const { ??? }
you better off keeping std::string
private member of newclass
. declare :
class newclass { private: std::string str; ... public: std::string getstring() const; }; ... std::string newclass::getstring() const { return str; }
however if need inherit std::string
do
std::string newclass::getstring() const { return *this; };
but warned cut out parts of derived class. better this:
const std::string& newclass::getstring() const { return *this; };
which return reference std::string newclass
has inherited from.
Comments
Post a Comment