Hi,
So the default constructor has a pre-initialisation list. mSize (size) using the arguments. If I had a TCHAR / WCHAR / C String pass as an argument, would I be able to copy that in the pre-initialisation list as the std::size_t is? |
It is called a member initialization list, and it uses direct initialization for all the member variables. So one can use this to initialise any member variable, provided that the type of the argument and the member match.
Can anyone could expand on why he chose to write it as such and how / why / what happens? When would I use this in my code? |
Yes, it is a short form of if then else. It is being used here in the member initialization list, because I am not sure one can't have if statements there. Used elsewhere, it's a short cut when the expressions are simple.
Is also the reason this code is so concise is partly it being a single *.cpp file and not split into separate header / cpp file? I think the reason he has done this is to shorten the code as much as possible. |
Yes, probably. I prefer to always split into .cpp and .hpp files. That way clients won't see any actual code.
Would I do any pre-initialisation on those variables or any checks via the pre-initialisation? |
I didn't think there was a need to scope all the member variables like that, any member function has direct access to member variables. Is that a Windows thing? I am a Linux guy :+)
It's always a good idea to use a member initialization list. Doing assignment like that creates unrelated unnamed temporaries, then copies them to variables that have already been default initialised.
Although I note that there is the
wcscpy_s
function call in there and a call to the member function
setStringMonitorNameAndDescription
, I guess you could use the
member initialization list to do all the other members, then call those functions in the body of the ctor. I wonder why that function call is there? There must be a reason they can't do:
szDeviceName(lpMONITORINFOEX->szDevice),
or any checks via the pre-initialisation? |
My instinct is to do validation in the ctor body and
throw
an exception if there is a problem. One can also have a
try
in the member initialization list. But this is Windows code, so they may not be the right thing to do. Also exceptions might only be appropriate if one has them throughout the application.
Good Luck !!