Inherited Code Discussions | C++ | HackerRank

Inherited Code

  • + 3 comments

    That's an error! You returning a pointer to an object (int this case error that is destroyed after function call).

    That's really dangerous! You need to store the string inside the class so error string must be inside the class.

    class BadLengthException {
    public:
    	BadLengthException(int n) {
        	stringstream ss;
        	ss << n;
        	m_error = ss.str();
        }
        
    	const char* what() const throw() {
        	return m_error.c_str();
    	}
        
    private:
        string m_error;
    };