警告C26495函数从构造函数调用到初始化变量,为什么? [英] Warning C26495 function invocation from constructor to initialize variable, why?

查看:929
本文介绍了警告C26495函数从构造函数调用到初始化变量,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:在VS中启用Microsoft本机推荐规则.

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.

警告C26495:变量'Person :: m_id'未初始化.始终初始化成员变量(类型6).

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*/
    }

};

推荐答案

您收到警告,因为您没有在构造函数中初始化成员或使用初始化列表.您在函数调用中执行操作的事实不会被捕获",因为它不会为每个简单的警告检查而检查每个函数调用.它很快就会变成一堆巨大的要检查的函数调用.

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.

您可能不会收到有关字符串的警告.这是因为当您不对其进行初始化时,它使用的是其默认构造函数,该构造函数构造一个空字符串,长度为零个字符.因此,您的字符串实际上已初始化.但是对于 int 这样的基本类型,没有这样的默认构造函数"

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/字符串/

这篇关于警告C26495函数从构造函数调用到初始化变量,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆