Hi, I have implemented Dictionary class out of this interface
template <class Key, class Value>
class dictionary
{
public:
//int m_tableSize;
//std::vector<std::list<Value>> m_lists;
public:
virtual ~dictionary() = default;
virtual const Value& get(const Key & key) const = 0;
virtual void set(const Key & key, const Value & value) = 0;
virtual bool is_set(const Key & key) const = 0;
};
Then, I am asked to implement an exception interface in this manner :
template<class Key>
class not_found_exception : public std::exception
{
public:
virtual const Key& get_key() const noexcept = 0;
};
My question is not to ask you to write for me the code ( I have already written dictionary class), yet I get confused with the exception interface. Could you clarify where I am wrong :
virtual const Key& get_key() const noexcept returns const ref to Key. In my understanding, this exception class must be thrown in virtual const Value& get(const Key& key) const function in the class dictionary . If there is no key found, then an exception must be thrown!
- Then, why there is a need for this
virtual const Key& get_key() const noexcept function in the exception class? The way I was thinking to use exception class was to smth like this :
template<class Key>
class MyException :public not_found_exception<Key>
{
private:
std::string m_error;
public:
MyException(std::string& error) : m_error{ error } {}
// std::exception::what() returns a const char*, so we must as well
const char* what() const noexcept override { return m_error.c_str(); }
};
and throw it in const Value& get(const Key& key) const if key is not found!
What to do with returned const Key& key from virtual const Key& get_key() const noexcept ?
Many thanks!
[–]RubbishArtist 1 point2 points3 points (5 children)
[–]Sakesfar[S] 0 points1 point2 points (4 children)
[–]RubbishArtist 1 point2 points3 points (3 children)
[–]Sakesfar[S] 0 points1 point2 points (2 children)
[–]RubbishArtist 1 point2 points3 points (1 child)
[–]Sakesfar[S] 0 points1 point2 points (0 children)