Reputation: 47
I am actually working on a project, which has one file like this.
#include "Preference.h"
#include <string>
#include <list>
#include <exception>
namespace MedicalStudentMatcher
{
class PreferenceException: public std::exception
{
public:
PreferenceException(std::string message) : m_message(message) {}
virtual ~PreferenceException() { }
virtual const char* what() const throw()
{
return m_message.c_str();
}
private:
std::string m_message;
};
class PreferenceReader
{
public:
PreferenceReader(std::string filename);
virtual ~PreferenceReader();
std::list<Preference<std::string, std::string>> ReadPreferences();
private:
std::string m_filename;
std::string trim(std::string str);
};
}
Now the questions are
1. How is the constructor working ? (Please bear in mind that I am a newbie to STL in C++ , and any kind of advanced methods in C++)
2. Explain the syntax of what() function.(Why there are two const then a char * and then a throw)
3. What does the below line mean
std::list<Preference<std::string, std::string>> ReadPreferences();
4. I want to traverse through this list. How do I go about it.?
list<Preference<string, string>> hospitalPrefs = hospitalReader.ReadPreferences();
list<Preference<string, string>> studentPrefs = studentReader.ReadPreferences();
list<Match<string, string>> matches;
5. How do template class work in the following case
and how is preference class using it. What is P m_preferrer declare ? How are "initialisation list" working in this case?
template <class P, class O>
class Preference
{
private:
P m_preferrer;
O m_preferred;
int m_value;
public:
Preference(const P& preferrer, const O& preferred, int value) : m_preferrer(preferrer), m_preferred(preferred), m_value(value) {}
virtual ~Preference() {}
P getPreferrer() const { return m_preferrer; }
O getPreferred() const { return m_preferred; }
int getValue() const { return m_value; }
};
template <class P, class O>
bool less_than(const Preference<P, O>& p1, const Preference<P, O>& p2)
{
return p1.getValue() < p2.getValue();
}
}
Even after thorough googling i couldn't find answer to these question.
Please help. if you need any more info on other files, kindly comment.
Upvotes: 1
Views: 92
Reputation: 249123
PreferenceException
constructor uses an "initialization list" to set m_message
. Now that you know that term, you can search for it to learn more.virtual const char* what() const throw()
declares "A virtual (runtime polymorphic) function which returns a pointer to (an array of) characters, where that pointer cannot be used to modify those characters." The trailing "const throw()" mean "This function cannot modify its implicit this
argument, i.e. it cannot modify the instance of the class on which it was called, and it cannot throw any exceptions."list<Preference<string, string>> hospitalPrefs = hospitalReader.ReadPreferences();
for (Preference<string, string>& pref : hospitalPrefs)
{
// do something with pref
}
Or if you're stuck on C++98 instead of C++11:
list<Preference<string, string>> hospitalPrefs = hospitalReader.ReadPreferences();
for (list<Preference<string, string>>::iterator it = hospitalPrefs.begin(); it != hospitalPrefs.end(); ++it)
{
// do something with pref
}
Upvotes: 2