Menu

Member initialization list order

Maximilian
2016-03-30
2016-04-03
  • Maximilian

    Maximilian - 2016-03-30

    I'm currently updating my cppcheck installation to version 1.7.2, now there are some new warnings. I was trying to figure out what is "best practise" but did'nt find anything.
    My original code looks like this:

    class Base
    {
        int memberOfBaseClass;
    
        Base (int val) : memberOfBaseClass(val) {}
    }
    
    class Derived : Base
    {
        double memberOfDerivedClass;
    
        Derived (int val1, double val2) : Base(val1), memberOfDerivedClass(val2) {}
    }
    

    Now, im getting an "uninitMemberVar"-warning, because "memberOfDerivedClass" is supposed to be not initialized (which is definitly wrong). If i'm changing the order of the initialization list from the derived class as follow, it works without warnings:

    class Derived: Base
    {
        double memberOfDerivedClass;
    
        Derived (int val1, double val2) : memberOfDerivedClass(val2), Base(val1) {}
    }
    

    But i had the opinion that its much more readable to pass the parameters in the order the constructors are called. Is this behavior a bug or am i simply wrong?

    Thanks for your help

     
    • Alexander Mai

      Alexander Mai - 2016-04-03

      I cannot reproduce with git head. Is that a stand-alone example? Otherwise it might have been fixed already.

       
  • Mr. X

    Mr. X - 2016-03-30

    This looks like a bug in cppcheck.

     

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.