views:

63

answers:

2

i just moved from C to C++, and now work with lists. i have a class called "message", and i need to have a class called "line", which should have a list of messages in its properties. as i learned, the object's properties should be initialized in the constructor's initialization list, and i had the "urge" to initialize the messages list in addition to the rest of the properties (some strings and doubles). is that "urge" justified? does the list need to be initialized?
thank you in advance.

here is my code.
the purpose is to create an empty list of lines, and the constructor i'm talking about is the one in line.cpp

//-------------------
//Code for line.h:
//-------------------

#ifndef LINE_H_
#define LINE_H_

#include "message.h"
#include <string>
#include <list>
using namespace std;

namespace test
{
    using std::string;

    class Line
    {
        public:
            // constractor with parameters
            Line(const string& phoneStr, double callRate, double messageRate);

            //function to get phone string
            string getPhoneStr() const;

            double getCallRate() const;

            double getMessageRate() const;

            double getLastBill() const;

            void addMessage(const string& phoneStr);


        private:
            string mPhoneStr;
            list<Message> mMessages;
            double mMessageRate;
            double mLastBill;
    };
}

#endif /* LINE_H_ */


//-------------------
//Code for line.cpp:
//-------------------

#include "line.h"

namespace test
{
    Line::Line(const string& phoneStr, double callRate, double messageRate)
        : mPhoneStr(phoneStr), mCallRate(callRate), mMessageRate(messageRate),
        mLastBill(0) {}

    //getters:

    string Line::getPhoneStr() const
    {
        return mPhoneStr;
    }

    double Line::getCallRate() const
    {
        return mCallRate;
    }

    double Line::getMessageRate() const
    {
        return mMessageRate;
    }

    double Line::getLastBill() const
    {
        return mLastBill;
    }


}


//-------------------
//Code for message.h:
//-------------------

#ifndef MESSAGE_H_
#define MESSAGE_H_

#include <string>

namespace test
{
    using std::string;

    class Message
    {
        public:
            // constractor with parameters
            Message(const string& phoneStr);

            //function to get phone string
            string getPhoneStr() const;

            //function to set new phone string
            void setPhoneStr(const string& phoneStr);


        private:
            string mPhoneStr;
    };
}
#endif /* MESSAGE_H_ */

//-----------------------------------------------------------------------

//---------------------
//code for message.cpp:
//---------------------


#include "message.h"

namespace test
{

    Message::Message(const string& phoneStr) : mPhoneStr(phoneStr) {}

    string Message::getPhoneStr() const
    {
        return mPhoneStr;
    }

    void Message::setPhoneStr(const string& phoneStr)
    {
        mPhoneStr = phoneStr;
    }

}
A: 

You don't have to do everything in the initialisation list. It's hard to tell without seeing some code, but it sounds like adding the messages would be better done in the body of the constructor.

anon
pardon me for posting it here too, but i still don't know how this site works. as soon as i get some answer, i'll delete it from the main message:this is my class: http://pastebin.com/X9Y9j0bH . when declared, a line should have an empty list of messages
bks
@bks Don't use pastebin - post the code as part of your question.
anon
i see. now pasted all the code, hope its clear enough
bks
+1  A: 

The initialization list is for initializing any base classes and member variables. The body of the constructor is meant to run any other code that you need before the object can be considered initialized.

I'm having a hard time understanding your situation, but hopefully the above helps.

GMan