Difference in Style and Organisation

I have seen many code written as below:

1
2
3
4
5
6
7
8
9
10
11
public:
	// (default) constructor
	dumb_array(std::size_t size = 0)
		: mSize(size),
		mArray(mSize ? new int[mSize]() : 0)
	{}
//...
private:
	std::size_t mSize;
	int* mArray;
};


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?

The second confusing thing is the conditional operator. Is that similar to:
1
2
3
4
5
6
7
8
		mArray = If(mSize)
	{
		new int[mSize]();
	}
	else
	{
		0;
	}

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?

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.

In my constructor of SingleMonitorInfo:
1
2
3
4
5
6
7
8
9
10
11
12
SingleMonitorInfo::SingleMonitorInfo(MONITORINFOEX* lpMONITORINFOEX)
{
	SingleMonitorInfo::dMaxPercentDifference = static_cast<double>(1);
	SingleMonitorInfo::intCheckTaskbar = static_cast<int>(1);
	SingleMonitorInfo::rcMonitorArea = lpMONITORINFOEX->rcMonitor;
	SingleMonitorInfo::rcWorkArea = lpMONITORINFOEX->rcWork;
	SingleMonitorInfo::dwStatusFlags = lpMONITORINFOEX->dwFlags;
	SingleMonitorInfo::dwCapabilitiesFlags = DWORD(0x00000000);
	wcscpy_s(SingleMonitorInfo::szDeviceName, 33, lpMONITORINFOEX->szDevice);
	SingleMonitorInfo::setStringMonitorNameAndDescription(lpMONITORINFOEX->szDevice);
	lpPixelArray = NULL;
}


Would I do any pre-initialisation on those variables or any checks via the pre-initialisation?
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 !!

TheIdeasMan wrote:
I wonder why that function call is there? There must be a reason they can't do:


I see from one of your other posts, the type is TCHAR array.

Another thought: Would it not be easier to have a lpMONITORINFOEX as a member variable in your class?
The reason I chose wcscpy_s is to try and copy the strings safely. I know this does not completely eradicate any risk, but I thought it was best.

The setMonitorNameAndDescription has to run another winAPI function (EnumDisplayDevices) to gain more information.

The type for the character strings is either TCHAR or WCHAR (I think WCHAR is best to be explicit) but strings still confuse me.

I could add MONITORINFOEX to my class, it might make things simpler. However it gives me more practice working with the bare variables themselves. It is the same reason I am trying to avoid things such as std::vector and winMFC. I want to learn "correctly", as one can be in programming, in a manner I can understand what happens at the lowest levels. This way maybe one day I will be able to understand more complex functions.

Also I was unsure what ctor was. More reading I guess, just reading confuses me with simple terminology. Hence why I failed university and keep asking stupid questions. ^^

The type for the character strings is either TCHAR or WCHAR (I think WCHAR is best to be explicit) but strings still confuse me.


Maybe these articles will help.

http://www.codeproject.com/Articles/2995/The-Complete-Guide-to-C-Strings-Part-I-Win32-Chara
http://www.codeproject.com/Articles/3004/The-Complete-Guide-to-C-Strings-Part-II-String-Wra
The reason I chose wcscpy_s is to try and copy the strings safely. I know this does not completely eradicate any risk, but I thought it was best.

The setMonitorNameAndDescription has to run another winAPI function (EnumDisplayDevices) to gain more information.


Yes I gathered that from your other post.

Also I was unsure what ctor was.


Constructor :+) Yep abbreviation sucks, I tell people not to do it in their code all the time :+) dtor means destructor, in case you see that somewhere. They are quite common abbreviations.

It is the same reason I am trying to avoid things such as std::vector and winMFC. I want to learn "correctly", as one can be in programming, in a manner I can understand what happens at the lowest levels. This way maybe one day I will be able to understand more complex functions.


While that is admirable, rather than avoid the STL I would suggest that you might be better off learning the detail of how the STL interacts with other things. There is plenty of learning there, and I gather possibly more useful. Once you have work coding, I imagine you would be expected to code using the STL.

Is your code Win32? I don't know, I am a Linux person. Just that I heard you don't have to code in Win32 these days. Perhaps a Windows person can help with that.

Was there a reason you scoped your member variables here?
SingleMonitorInfo::dMaxPercentDifference = static_cast<double>(1);

I don't see the reason for the cast either, could you have just done this in the initializer list?
dMaxPercentDifference(1.0),
My code is mostly pure C++ as far as I am aware. There are a few specific winAPI calls, use of MONITORINFOEX and WCHAR. I believe those are the only Windows specific bits of my code.

I am following Charles Petzoid's instructions from Programming Windows (Fifth Edition). Although this was based around Windows 95 / 98 and ME, it is supposed to be a very good book. It does explain in a lot of detail UNICODE and a lot of Microsoft's decisions in their code.

I don't know why the variables are scoped, I just like to be explicit. I did however remove the casts myself prior to this post. Again, I am not sure of why I did it this way, but I did...
Ok it sounds as though you are doing well, good luck in your future endeavours :+)
Topic archived. No new replies allowed.