关于结构的警告 C26495? [英] Warning C26495 on structures?
问题描述
过去一两年我一直在使用 C#/.NET,但我正在为客户端更新旧的 C++/MFC 应用程序.我知道自从我上次使用 C++ 以来已经发生了很多变化,但这种变化让我很失望.
I've been using C#/.NET for the past decade or two, but am updating an old C++/MFC application for a client. I know a lot has changed since I last worked with C++, but this one kind of threw me.
我有以下结构:
typedef struct _FILTERINFO {
int nCustomerID;
CString sCustomerName;
} FILTERINFO, *LPFILTERINFO;
Visual Studio 2019 给了我一个警告:
And Visual Studio 2019 gives me a warning:
警告 C26495 变量_FILTERINFO::nCustomerID"未初始化.总是初始化一个成员变量(type.6).
Warning C26495 Variable '_FILTERINFO::nCustomerID' is uninitialized. Always initialize a member variable (type.6).
那么,这里有什么问题,我将如何初始化变量?如您所见,我没有构造函数.我现在不需要它们用于结构,是吗?
So, what is the problem here and how would I initialize the variable? As you can see, I don't have a constructor. I don't need them for structures now do I?
注意:进一步研究这一点,我看到我的应用定义了其他 struct
,但由于某种原因没有收到同样的警告.它似乎特定于大约两个或三个声明.
NOTE: Looking into this further, I see my app defines other struct
that for some reason do not get this same warning. It seems specific to about two or three declarations.
推荐答案
去掉 typedef,你在做 C++,所以改写:
Drop the typedef, you are doing C++, so instead write:
struct FILTERINFO
{
int nCustomerID=0;
CString sCustomerName;
};
和
typedef FILTERINFO* LPFILTERINFO;
通常最好不要对指针进行 typedef,而是使用 FILTERINFO*
因为有时与 const
Normally it is better to not do a typedef of a pointer, use instead FILTERINFO*
because sometimes you get unexpected behavior when used with const
这篇关于关于结构的警告 C26495?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!