这个ctor /使用ctor是否正确? [英] Is this ctor / use of ctor correct?

查看:201
本文介绍了这个ctor /使用ctor是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  SingleMonitorInfo :: SingleMonitorInfo(MONITORINFOEX * lpMONITORINFOEX)
:rcMonitorArea(lpMONITORINFOEX-> rcMonitor),
rcWorkArea(lpMONITORINFOEX-> rcWork),
dwStatusFlags(lpMONITORINFOEX-> dwFlags),
szDeviceName({'\0'}),
szMonitorName({'\0'}),
szMonitorDescription \\ 0'}),
lpPixelArray(NULL)
{
wcscpy_s(SingleMonitorInfo :: szDeviceName,33,lpMONITORINFOEX-> szDevice);
SingleMonitorInfo :: setStringMonitorNameAndDescription(lpMONITORINFOEX-> szDevice);
}



我使用成员初始化列表重写我的程序,我希望上面的代码是正确的,虽然这是我第一次使用成员初始化列表。



我意识到,以后,在这个类的父类:我创建另一个副本,的副本创建(实例化我想)的列表。当我想我需要的是:

  for(int i = 0; i  iMaximumSize; i ++) 
{
smiMonitorList [i] = SingleMonitorInfo(& lpMonitorList-> infoArray [i]);
}

看起来太简单,容易出现验证错误,如何在成员初始化中做简单检查?从我的理解成员初始化列表可以做逻辑if语句(X?A:B),虽然我不知道如何验证输入。



编辑:我有一个默认的ctor和重载的MONITORINFOEX

  SingleMonitorInfo :: SingleMonitorInfo()
:dMaxPercentDifference(1)
iCheckTaskbar(1),// int
rcMonitorArea(RECT {0,0,0,0}),// RECT
rcWorkArea(RECT {0,0,0,0}) ,// RECT
dwStatusFlags(DWORD(0x00000000)),// DWORD
dwCapabilitiesFlags(DWORD(0x00000000)),// DWORD
szDeviceName({'\0' / String(WCHAR)
szMonitorName({'\0'}),// String(WCHAR)
szMonitorDescription({'\0'}),// String(WCHAR)
lpPixelArray(NULL)// unsigned char *

{



}

解决方案

不需要在成员初始化列表中执行检查,您可能需要重新考虑您的设计。



让我们看看你的类:

  SingleMonitorInfo :: SingleMonitorInfo(MONITORINFOEX * lpMONITORINFOEX)
:rcMonitorArea(lpMONITORINFOEX-> rcMonitor),
rcWorkArea(lpMONITORINFOEX-> rcWork),
dwStatusFlags(lpMONITORINFOEX - > dwFlags),
szDeviceName({'\0'}),
szMonitorName({'\0'}),
szMonitorDescription({'\0'}) ,
lpPixelArray(NULL)

这里我们可以看到一个很大的问题,如果 lpMONITORINFOEX nullptr 。它看起来像你的类需要它的功能。问自己:我可以有没有 MONITORINFOEX 对象的 SingleMonitorInfo 类吗?



如果你的类需要,通过传递一个引用使它更明确。

  SingleMonitorInfo :: SingleMonitorInfo(const MONITORINFOEX& lpMONITORINFOEX)
:rcMonitorArea(lpMONITORINFOEX.rcMonitor),
rcWorkArea(lpMONITORINFOEX.rcWork),
dwStatusFlags(lpMONITORINFOEX.dwFlags),
szDeviceName({'\0'}),
szMonitorName({'\0'}),
szMonitorDescription({'\0'}),
lpPixelArray b $ b

您的类的用户无法再构造错误的对象。如果因为某些原因 lpMONITORINFOEX 是可选的,并且需要保留一个指针,可以简单地使用三元运算符?:

  SingleMonitorInfo :: SingleMonitorInfo(MONITORINFOEX * lpMONITORINFOEX)
:rcMonitorArea(lpMONITORINFOEX?lpMONITORINFOEX-> rcMonitor:nullptr) ,
rcWorkArea(lpMONITORINFOEX?lpMONITORINFOEX-> rcWork:false),
dwStatusFlags(lpMONITORINFOEX?lpMONITORINFOEX-> dwFlags:0),
szDeviceName({'\0'
szMonitorName({'\0'}),
szMonitorDescription({'\0'}),
lpPixelArray(NULL)

显然我不知道你的对象是什么,所以我只是猜测默认输入。 $ b

    SingleMonitorInfo::SingleMonitorInfo(MONITORINFOEX* lpMONITORINFOEX)
    :rcMonitorArea(lpMONITORINFOEX->rcMonitor),
    rcWorkArea(lpMONITORINFOEX->rcWork),
    dwStatusFlags(lpMONITORINFOEX->dwFlags),
    szDeviceName({ '\0' }),
    szMonitorName({ '\0' }),
    szMonitorDescription({ '\0' }),
    lpPixelArray(NULL)
{
    wcscpy_s(SingleMonitorInfo::szDeviceName, 33, lpMONITORINFOEX->szDevice);
    SingleMonitorInfo::setStringMonitorNameAndDescription(lpMONITORINFOEX->szDevice);
}

I am rewriting my program using member initialisation lists, I hope the code above is correct, although this is the first time I've used member initialisation lists.

I realised that later on, in a parent of this class: I was creating another copy, of a copy to create (Instantiate I think) the list. When all I think I needed was:

for (int i = 0; i < lpMonitorList->iMaximumSize; i++)
    {
        smiMonitorList[i] = SingleMonitorInfo(&lpMonitorList->infoArray[i]);
    }

It seems too simple and prone to verification errors, would this example work, and how to I do simple checks in the member initialisation? From what I understand the member initialisation list can do logical if statements ( X ? A : B ), though I cannot fathom how I would verify input.

EDIT: I have a default ctor and overloaded for MONITORINFOEX

SingleMonitorInfo::SingleMonitorInfo()
:dMaxPercentDifference(1), // Double
iCheckTaskbar(1), // int
rcMonitorArea(RECT{ 0, 0, 0, 0 }), // RECT
rcWorkArea(RECT{ 0, 0, 0, 0 }), // RECT
dwStatusFlags(DWORD(0x00000000)), // DWORD
dwCapabilitiesFlags(DWORD(0x00000000)), // DWORD
szDeviceName({ '\0' }), // String (WCHAR)
szMonitorName({ '\0' }), // String (WCHAR)
szMonitorDescription({ '\0' }), // String (WCHAR)
lpPixelArray(NULL) // unsigned char*

{

}

解决方案

It shouldn't be necessary to perform checks in your member initializer list, and if you need to perform checks, you probably need to rethink your design.

Let's look at your class:

SingleMonitorInfo::SingleMonitorInfo(MONITORINFOEX* lpMONITORINFOEX)
:rcMonitorArea(lpMONITORINFOEX->rcMonitor),
rcWorkArea(lpMONITORINFOEX->rcWork),
dwStatusFlags(lpMONITORINFOEX->dwFlags),
szDeviceName({ '\0' }),
szMonitorName({ '\0' }),
szMonitorDescription({ '\0' }),
lpPixelArray(NULL)

Here we can see a pretty big problem if lpMONITORINFOEX is nullptr. It looks like your class needs it to function though. Ask yourself: could I have a SingleMonitorInfo class without a MONITORINFOEX object?

If your class needs that object, make it more explicit by passing a reference!

SingleMonitorInfo::SingleMonitorInfo(const MONITORINFOEX& lpMONITORINFOEX)
:rcMonitorArea(lpMONITORINFOEX.rcMonitor),
rcWorkArea(lpMONITORINFOEX.rcWork),
dwStatusFlags(lpMONITORINFOEX.dwFlags),
szDeviceName({ '\0' }),
szMonitorName({ '\0' }),
szMonitorDescription({ '\0' }),
lpPixelArray(NULL)

Users of your class can no longer construct your object incorrectly. If for some reason lpMONITORINFOEX was optional, and needed to remain a pointer, you could simply use the ternary operator ?:

SingleMonitorInfo::SingleMonitorInfo(MONITORINFOEX* lpMONITORINFOEX)
:rcMonitorArea(lpMONITORINFOEX ? lpMONITORINFOEX->rcMonitor : nullptr),
rcWorkArea(lpMONITORINFOEX ? lpMONITORINFOEX->rcWork : false),
dwStatusFlags(lpMONITORINFOEX ? lpMONITORINFOEX->dwFlags : 0),
szDeviceName({ '\0' }),
szMonitorName({ '\0' }),
szMonitorDescription({ '\0' }),
lpPixelArray(NULL)

Obviously I don't know what your objects are, so I'm just guessing default input.

这篇关于这个ctor /使用ctor是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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