添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Note: Enable Microsoft Native Recommended Rules in VS.

I am not initializing data members in constructor/initializer list, instead due to too many data members, these data members are copied within a function and then invoking function from constructor.

Is there any specific reason for this warning message.

warning C26495: Variable 'Person::m_id' is uninitialized. Always initialize a member variable (type.6).

class Person
    std::string m_name;
    int m_id; 
    /* Other data members*/
public:
    Person()
        initialize();
    void initialize()
        m_name = "someText";
        m_id = 1;
        /* Other data members initialization*/
                Do you get the same warning/error with just these two data members (as shown in this code)? Just wondering how to try to reproduce this
– doctorlove
                Feb 25, 2019 at 12:23
                @melpomene I agree that initializer list is definitely a better choice. Lets say if there  are too many data members, then it looks bad.
– Innovative Thinkers
                Feb 25, 2019 at 12:30

You get the warning because you don't initialize the member in the constructor or use an initializer list. The fact that you do it in a function call isn't "captured" since it won't check each function call for this simple warnings check. It could quickly become a huge tree of function calls to check.

You probably don't get a warning for your string. This is because when you don't initialize it, it is using its default constructor which constructs an empty string, with a length of zero characters. So your string actually IS initialized. But for basic types like int there is no such default "constructor"

http://www.cplusplus.com/reference/string/string/string/

@YvesDaoust actually an interesting question. It probably depends on whether the check is done before or after the inline functions are being expanded (copy pasted for simple terms). If after, then the check should see the initialization. But if the check is done before it would not matter. I would GUESS that this is compiler specific and that the standard does not mandate when/or even if a check like this is performed. – inquam Feb 25, 2019 at 13:12 @YvesDaoust: On VS 2017 it doesn't matter if you define inline. And the code analysis is probably a VS specific implementation. Could very well be a program that runs that is not part of the compiler at all that does the analysis. Compiling in C++ Builder 10.3.1 gives no warning at all. Same is true for VS if you don't turn on Project -> Properties -> Code Analysis -> General -> Enable Code Analysis on Build – inquam Feb 25, 2019 at 13:20 On VS 2017, pre-analysis is performed in the text editor, to highlight issues; this is done by an alternative tool. But I would be surprised that code inlining would be performed by another entity than the compiler. – user1196549 Feb 25, 2019 at 13:25

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.