在构造函数的初始化列表中启动列表

发布于 2024-09-05 00:17:30 字数 2735 浏览 2 评论 0原文

我刚刚从 C 迁移到 C++,现在使用列表。 我有一个名为“message”的类,我需要一个名为“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;
    }

}

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?

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;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-09-12 00:17:30

初始化列表用于初始化任何基类和成员变量。构造函数的主体旨在运行您需要的任何其他代码,然后才能将对象视为已初始化。

我很难理解您的情况,但希望以上内容对您有所帮助。

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.

咋地 2024-09-12 00:17:30

您不必执行初始化列表中的所有操作。如果没有看到一些代码,很难说清楚,但听起来最好在构造函数的主体中添加消息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文