c++ - uninit_member : Non-static class member m_wszParams is not initialized in this constructor nor in any functions that it calls in C++

标签 c++ c++11

我有 IVerification 类,这个类有两个成员变量(m_wszParams 和 m_wszParamType)。在这个类的构造函数中,我没有初始化这些成员变量。稍后,在派生类(MyReader)中初始化这些变量。

因此,我收到警告“uninit_member:非静态类成员 m_wszParams 未在此构造函数中或它调用的任何函数中初始化。”

要解决此警告,我可以在 IVerification 构造函数中使用 nullptr 初始化这些成员变量,如下所示吗?

IVerification::IVerification()
{
    m_wszParams = nullptr;
    m_wszParamType = nullptr;
}

完整代码如下:

.h文件

class IVerification
{

public:
    IVerification();
    virtual ~IVerification(); 

protected:
    wchar_t* m_wszParams;
    wchar_t* m_wszParamType;
}

.cpp 文件

IVerification::IVerification()
{

}

下面是派生类

MyReader.h 文件

class MyReader : public IVerification
{
public:
    MyReader();
    ~MyReader();
public:
    void SetParams(wchar_t* wszParams, wchar_t* wszParamType);
}

MyReader.cpp 文件

void MyReader::SetParams(wchar_t* wszParams, wchar_t* wszParamType)
{
    m_wszParamType = wszParamType;
    m_wszParams = wszParams;
}

最佳答案

至少

class IVerification
{
public:
    IVerification() = default;
    virtual ~IVerification() = default;

protected:
    wchar_t* m_wszParams = nullptr;
    wchar_t* m_wszParamType = nullptr;
};

下一步:指针不是你的 friend ,使用 std::string。

关于c++ - uninit_member : Non-static class member m_wszParams is not initialized in this constructor nor in any functions that it calls in C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57800545/

相关文章:

c++ - 为基类重载 >> 运算符

C++ 为什么不能在 constexpr 函数中调用字符串大小函数

C++11 实验,为什么我不能使用某些功能?

c++ - C++ 程序的程序终止是可观察的行为吗?

c++ - 如何通过引用 O(1) 在 C++ 中复制 vector ?

C++如何为 map 制作浅拷贝构造函数

c++ - 使用指针写入 strcat() 时出错

c++ - 模板相关的类型定义

没有序列点的 C++11?

c++ - 通过 cmake 文件在 Eclipse 中设置编译器特定设置和项目构建位置