Help with an assignment question.

i'm having problems creating and implementing the final requirment:void remove_message(int i) const;

This is my question:

You have to create a class called mailbox. You don't yet know how to store a collection of message objects. Instead, use the following brute force approach: The mailbox contains one very long string, which is the concatenation of all messages. You can tell where a new message starts by searching for a From: at the beginning of a line. This may sound like a dumb strategy, but surprisingly, many e-mail systems do just that.
Implement the following member functions:
void Mailbox::add_message(Message m);
Message Mailbox::get_message(int i) const;
void remove_message(int i) const;


I would appreciate any input or comments anyone can make about what I have so far and if what I am doing or trying to do makes sense.


class Mailbox
{
public:
Mailbox(string u);
void add_message(Message m);
Message get_message(int i) const;
int count_messages() const;
string get_user() const;
private:
vector<Message> messages;
string user;
};

Mailbox::Mailbox(string u)
{
user = u;
}

string Mailbox::get_user() const
{
return user;
}

void Mailbox::add_message(Message m)
{
messages.push_back(m);
}

Message Mailbox::get_message(int message_num) const
{
return messages[message_num];
}

int Mailbox::count_messages() const
{
return messages.size();
}
The code makes sense. The vector class lets you add messages to the end of the list efficiently. If this is the typical operation, the code makes sense. Removing a message in the middle of the vector might be expensive though.

However, this doesn't make sense:

void remove_message(int i) const

A member function that changes the object (removing a message in this case) should not be declared const.
Last edited on
Topic archived. No new replies allowed.