【问题标题】:How to update the attributes of an object?如何更新对象的属性?
【发布时间】:2019-05-11 08:51:09
【问题描述】:

我有一个Aircraft 课程。我想循环更新飞机对象的属性(每隔一秒)。

如何在不创建新对象的情况下更新这些属性?我应该使用指针吗?我的目标:每秒钟更新一次飞机信息

我的代码:

class Aircraft
{
public:
    unsigned int altitude, speed, direction;

    Aircraft(unsigned int aAltitude, unsigned int aSpeed, unsigned int aDirection) 
    {
        altitude = aAltitude;
        speed = aSpeed;
        direction = aDirection;
    }
};

int main()
{
    //aircraft's initial values
    Aircraft myAircraft(0, 10, 345); 
    //Initial values should are printed 
    cout << myAircraft.altitude + "," + myAircraft.speed + "," + myAircraft.direction << endl; //print initial values

    //In this loop new values for altitude, speed and direction should be assigned
    for (int i = 0; i < 10; i++)
    {
        //aircraft's new values
        Aircraft myAircraft(new altitude, new speed, new direction);

        //print updated attributes
        cout << myAircraft.altitude + "," +
                myAircraft.speed "," myAircraft.direction
             << endl << endl; //print new values
    }
}

结果应如下所示:(值无关紧要)

0, 10, 345
0, 30, 345
0, 60, 345
0, 100, 345
0, 150, 345
300, 180, 345
700, 220, 345
2000, 250, 345

【问题讨论】:

    标签: c++ algorithm class member


    【解决方案1】:

    如何在不创建新对象的情况下更新这些属性?

    在循环外创建Aircraft,使用类的setter functions(建议将类资源设为private),设置类内的各个属性。

    我应该使用指针吗?

    给出解释,正确地NOT,因为您只想打印类属性中的每个更新,不需要动态内存分配。

    关于更新后每次打印元素,正常的C++约定是overload operator&lt;&lt;,方便你写:

    std::cout << aircraft_object;
    

    示例代码如下所示:(See Live)

    #include <iostream>
    
    using uint32 = unsigned int;
    class Aircraft /* final */
    {
    private: // private attributes
        uint32 altitude, speed, direction;
    
    public:
        // provided default arguments, so that default-construction is possible
        Aircraft(uint32 aAltitude = 0, uint32 aSpeed = 0, uint32 aDirection = 0)
            : altitude{ aAltitude }
            , speed{ aSpeed }
            , direction{ aDirection }
        {}
        // provide setters
        void setAltitude(const uint32 alti) noexcept { altitude = alti; }
        void setSpeed(const uint32 sp) noexcept { speed = sp; }
        void setDirection(const uint32 dir) noexcept { direction = dir; }
        // non-member function(s): operator<< overload
        friend std::ostream& operator<<(std::ostream& out, const Aircraft& obj) noexcept;
    };
    
    std::ostream& operator<<(std::ostream& out, const Aircraft& aircraft) noexcept
    {
        return out << aircraft.altitude << ", " << aircraft.speed
                                        << ", " << aircraft.direction << '\n';
    }
    
    int main()
    {
        Aircraft aircraft_obj{}; // constructed with intial values {0, 0,0}
    
        for (auto i = 0; i < 3; ++i)
        {
            uint32 alti, sp, dir;
            // get the user inputs
            std::cin >> alti >> sp >> dir;
            // set the attributes
            aircraft_obj.setAltitude(alti);
            aircraft_obj.setSpeed(sp);
            aircraft_obj.setDirection(dir);
            // print out the object
            std::cout << aircraft_obj;
        }
        return 0;
    }
    

    输入:

    0 10 345
    0 30 345
    0 60 345
    

    输出:

    0, 10, 345
    0, 30, 345
    0, 60, 345
    

    【讨论】:

    • line 21: " // 非成员函数:operator
    • @claridade 您是否阅读了提供的链接? docs.microsoft.com/en-us/cpp/standard-library/…。它将类Aircraft 的输出流operator&lt;&lt; 声明为friend 函数(它们是非成员函数)。正如我在答案中提到的,这可以帮助您简单地编写std::cout &lt;&lt; aircraft_obj;,这是一种更方便的方式,而不是一遍又一遍地输入整个内容。
    【解决方案2】:

    您可以使用. 运算符轻松修改非常量对象值。

    myAircraft.altitude  = newAltitude;
    myAircraft.speed     = newSpeed;
    myAircraft.direction = newDirection;
    
    cout << myAircraft.altitude << ", " << myAircraft.speed << ", " << myAircraft.direction << '\n';
    

    附:通过将字符串与+ 连接来打印字符串是一种非常糟糕的做法。 相反,请使用&lt;&lt; 运算符,如上所示。

    在您的情况下,代码甚至无效,因为您尝试添加带有字符串的数字。做你想做的事情的逻辑正确方法是首先将数字转换为字符串to_string(myAircraft.altitude) + ", "。

    另外,避免使用endl,因为它会不必要地刷新缓冲区。好处可以在这里看到:Benchmark。

    【讨论】:

    • “通过将字符串与 + 连接来打印字符串是一种非常糟糕的做法” 在这种情况下,这不是糟糕的做法,而是完全错误的。数值和字符串无论如何都不能连接。
    • 编辑了答案以包含您的见解。
    猜你喜欢
    • 2016-08-08
    • 1970-01-01
    • 2013-01-18
    • 2017-07-12
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 2012-03-16
    相关资源
    最近更新 更多