Menu

performance result : useInitializeList referring to copy constructor

2020-05-27
2020-05-27
  • Glenn Storm

    Glenn Storm - 2020-05-27

    I hesitate to actually draft a Trac ticket. A class copy constructor is used in my project, and the result found suggested using an initializer list to populate various members. This broke the copy constructor, so I'm pretty confident this result suggestion is not correct.

     
  • Daniel Marjamäki

    thanks for reporting. let's try to fix it. could you create a code example. I think the easiest approach would be that you bisect your code into a small example. It doesn't have to compile; if Cppcheck complains then I am happy.

     
  • Glenn Storm

    Glenn Storm - 2020-05-27

    :) Sure, I'd be happy to. There was another issue I notice by chance, unrelated, that I can open another discussion about.

    This result is an example of several within the same copy constructor: src\Tank.h|174|useInitializationList : performance : When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'controller' a value by passing the value to the constructor in the initialization list.|

    Points to this header file content:

    class Tank {
    public:
        unsigned int tankID = 0;
        TankController controller;
        // ...
    private:
        bool m_active = true;
        float m_posX, m_posY, m_baseR, m_turretR;
        // ...
    public:
        Tank() { TankInit(); }
        Tank( const TankController& control, const float& xPos, const float& yPos, const float& rot, const float& scale )
        {
            TankInit();
            SetController(control);
            SetPosition(xPos, yPos);
            SetBaseRotation(rot);
            // ...
        }
        Tank( const Tank &t )
        {
            tankID = t.tankID;
            controller = t.controller;
            // ...
            m_posX = t.m_posX;
            m_posY = t.m_posY;
            m_baseR = t.m_baseR;
            m_turretR = t.m_turretR;
            // ...
        }
    private:
        // ...
    }
    

    You'll find the copy constructor under the two constructor methods. Tank( const Tank &t ) { } Line 174 points to "controller = t.controller;", and the other similar results pointed out several (not all) other copy lines in this copy constructor.

    And in 'fixing' by utilizing an initializer list, the resulting crash seemed to indicate this was not an appropriate fix/result.

    'Hope that helps.

     

    Last edit: Glenn Storm 2020-05-27
  • Daniel Marjamäki

    is it open source? I do not understand why it would crash when moving it to an initializer list. is there something special about TankController? can you explain that?

     
  • Glenn Storm

    Glenn Storm - 2020-05-27

    Well, no, nothing special about TankController. And, as mentioned, there were around 12 other similar lines of "make use of an initialize list". I did and it crashed, and so I simply un-did. But, given your reaction. I'll revisit and report back.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.