2010-06-12 157 views
0

我刚刚从C迁移到C++,现在使用列表。 我有一个名为“消息”的类,我需要一个名为“line”的类 ,它应该在其属性中包含消息列表。据我所知,对象的属性应该在构造函数的初始化列表中初始化,并且除了其他属性(某些字符串和双精度)之外,我还有“敦促”来初始化消息列表。这种“冲动”是合理的吗?该列表是否需要初始化?在构造函数的初始化列表中启动列表

这是我的代码。
的目的是创建线的空列表,和我谈论的构造是一个在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; 
    } 

} 

回答

0

您不必做初始化列表中的一切。没有看到一些代码很难说,但听起来好像在构造函数的主体中添加消息会更好。

+0

原谅我在这里发布它,但我仍然不知道这个网站是如何工作的。只要我得到一些答案,我会从主要信息中删除它: 这是我的课程:http://pastebin.com/X9Y9j0bH。当声明时,一行应该有一个空的邮件列表 – bks 2010-06-12 09:11:01

+0

@bks不要使用pastebin - 将代码发布为问题的一部分。 – 2010-06-12 09:12:10

+0

我明白了。现在粘贴了所有的代码,希望它足够清晰 – bks 2010-06-12 09:20:59

2

初始化列表用于初始化任何基类和成员变量。构造函数的主体是为了在对象可以被初始化之前运行你需要的任何其他代码。

我很难理解你的情况,但希望上面的帮助。

相关问题