473,804 Members | 3,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

logical const and event programming

LuB
I'm writing a Win32 application - and more specifically, doing event
programming.

I want the application to be const compliant but I'm faced with a bit
of a conundrum.

Physically, many of my window methods can indeed be const Why? Many
Win32 calls send msg to a WNDPROC - or event handler. Therefore, a
const method may actually change something about a window. The 'value'
of const is lost here.

For example, to move a window - one can call ::SetPosition(h wnd, ...).
Assuming hwnd_ is a private instance member, If I wrap that call in a
class method, would I write

void SetPosition(... ) const
{
::SetPosition(h wnd_, ...);
}

or

void SetPosition(... )
{
::SetPosition(h wnd_, ...);
}

Either would work. Logically, its not really a const method since its
changing a property of the window - namely, its position. But
physically, it can be labelled as const because of the inherent
indirection in event programming.

The answer must applicable/general enough to handle/work with _any_
message.

Just curious what the consensus is.

Thanks,

-Luther

Mar 29 '06 #1
3 1606
LuB wrote:
I'm writing a Win32 application - and more specifically, doing event
programming.

I want the application to be const compliant but I'm faced with a bit
of a conundrum.

Physically, many of my window methods can indeed be const Why? Many
Win32 calls send msg to a WNDPROC - or event handler. Therefore, a
const method may actually change something about a window. The 'value'
of const is lost here.

For example, to move a window - one can call ::SetPosition(h wnd, ...).
Assuming hwnd_ is a private instance member, If I wrap that call in a
class method, would I write

void SetPosition(... ) const
{
::SetPosition(h wnd_, ...);
}

or

void SetPosition(... )
{
::SetPosition(h wnd_, ...);
}

Either would work. Logically, its not really a const method since its
changing a property of the window - namely, its position. But
physically, it can be labelled as const because of the inherent
indirection in event programming.

The answer must applicable/general enough to handle/work with _any_
message.

Just curious what the consensus is.


To be honest, I don't know what the consensus is. Most likely, if your
'SetPosition' is a member (and only a member can be const, right?), you
only need to make it non-const if it does indeed change the _object_
for which it's called. Now, in your example 'SetPosition' is a very
thin wrapper over an OS API call. That's your choice. In real life,
perhaps it would make sense to cache the size information in the object
(so you don't have to bother the OS when you need to know the position
of the window, for example). If that's the design, then setting the
position of a window will require that the member is non-const because
it's going to update some members.

Another consideration is the appearance of the design. You can choose
to designate certain member functions non-const _simply_because _ they
are logically such. That should allow you to impose some strictness on
your own programming style when 'SetPosition' cannot be called from,
say, 'GetText' (a contrived example, I concede, but sufficient).

Generally, it's up to you. Common sense should be your guide here.

V
--
Please remove capital As from my address when replying by mail
Mar 29 '06 #2
* LuB:
I'm writing a Win32 application - and more specifically, doing event
programming.

I want the application to be const compliant but I'm faced with a bit
of a conundrum.

Physically, many of my window methods can indeed be const Why? Many
Win32 calls send msg to a WNDPROC - or event handler. Therefore, a
const method may actually change something about a window. The 'value'
of const is lost here.

For example, to move a window - one can call ::SetPosition(h wnd, ...).
Assuming hwnd_ is a private instance member, If I wrap that call in a
class method, would I write

void SetPosition(... ) const
{
::SetPosition(h wnd_, ...);
}

or

void SetPosition(... )
{
::SetPosition(h wnd_, ...);
}

Either would work. Logically, its not really a const method since its
changing a property of the window - namely, its position. But
physically, it can be labelled as const because of the inherent
indirection in event programming.

The answer must applicable/general enough to handle/work with _any_
message.

Just curious what the consensus is.


I don't know about the consensus, but constness is a tool that's part of
a larger toolset, and is only meaningful when those other tools are also
applied. For example, having

void setPosition( Position pos ) { ... }

implies also having

Position position() const { ... }

where having setPosition() non-const is so that the client code can be
assured it won't mess around with a window it has declared const, while
having position() const is so that the client code can obtain the
position of a window it has declared const. It does not matter that
setPosition can be implemented as a C++-level const function. Because
it can /always/ be implemented technically as a C++-level const
function, that has nothing to do with event programming, e.g.

// How to absolutely not to do things... ;-)
class Window
{
private:
int* myXPos;
public:
Window(): myXPos( new int(666) ) {}
~Window() { delete myXPos; }

void setX( int x ) const { *myXPos = x; } // Uhuh, 'const'!

int x() { return *myXPos; } // Double uhuh, no 'const'!
};

Here all is well as long as the client code doesn't declare a Window as
const. But if the client code does, it finds that it can change the
Window's x-position (hey, wasn't that what 'const' should help guard
against?), and that it can't obtain the current x-position (hey,
shouldn't that be possible regardless of 'const' or not?). So to
support natural expectations for 'const', making 'const' a practically
useful tool for the client code, do the opposite of the above.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 29 '06 #3
LuB
I hope google doesn't top post .... but thanks to both of you for your
suggestions.

To follow up, one more scenerio rears its head at times. I am wondering
if this usage falls outside the conceptual tools Alf speaks of when
using const. (The underlying reasons for my questions - is because I
generally write everything const correct unless I have to change it -
so everything is const unless it cannot be). On that note, I'm sure
that in the right hands, the immutable keyword can be used where
appropriate - while in the wrong hands, it can be terribly abused,
confusing and incorrect.

Lets assume I want to paint something everytime a window is resized. I
have a class thinly wrapping a window and its related functionality.
Lets say the windows OnPaint handler member method is about 75 lines
long. In general, the OnPaint method asks its children for their
dimensions (const correctly) and then paints them. Generally, the
window doesn't maintain any state in this method ... but there just so
happens to be one measurment that the parent window *does* need to keep
track of. In this case, its the placement of an invisible splitter.
Lets just say that the splitter's position changes everytime the window
is resized - but otherwise, everything stays the same in this fairly
lengthy method.

I am opting to make this member _mutable_ and the OnPaint method const.

It seems that this is harmless and lets me call OnPaint from any const
windows - which I make it a practice to use as often as possible. It
seems I've judged that there is _important_ state like attached
children windows and less important state like where a splitter gets
pushed around to and furthermore, the less important state (or
incidental state) can be declared mutable when it seems harmless to do
so.

Would this practice be frowned upon in a commerical product? I would
intuit that using mutable should be done very infrequently - but is
fine when appropriate.

Thanks again,

-Luther

Mar 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4020
by: muser | last post by:
Can anyone run this program through their compiler or if they can see a logical error please point it out. I have my tutor working on it at the moment but I would rather a less ambigious response than the one I think he will provide. The test data, A:\\"514650.txt", appears at the end of the program. The problem is that the program won't read this file and the debugger as far as i know how to work it won't point out the mistake of any....
7
2906
by: Charles Crume | last post by:
Hello all; I have used dBASE, and other computer languages/databases, for years. They all have a logical field type. However, the version of MySQL used by the ISP hosting my site does not support a "logical" field type. It does support ENUM and I have set some up in a couple of tables that accept the values 'T' and 'F'. Sometimes they work like a logical field: if ($myrow) echo 'New';
3
2240
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. Someone on the SuSE programming mailing list suggested my problem is that I'm trying to execute a function (I assume he meant the constructor) at compile time. The same source code compile if I don't try to split it up into separate libraries. ...
2
9845
by: Marathoner | last post by:
I need to get the list of logical drive letters for mapped drives as well as the UNC or share name of the drive. I can obtain an array of the drive letters but it is the assiciated name that I cannot obtain. Example: C$ on ISDEVL2 If this is the X drive, I can get X:\ but cannot get 'C$ on ISDEVL2'. I have tried everything I know to do and would greatly appreciate any assistance anyone can provide. -- Robert Hill Senior...
8
2434
by: Chua Wen Ching | last post by:
Hi, I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming? I am not sure why i need to use it? I had some samples of c# codes using it. Can someone share their experiences why someone should use those operators and what type of scenarios? Any good snippets and example?
2
4937
by: Al Sav | last post by:
Hi, After enumerating the logical drives, using GetLogicalDrives, I need to know the Drive Types, ie. Floppy or Network. Is it possible in C# to do this? Thanks in advance. Alwin S.
9
1797
by: Ben Voigt | last post by:
Found another variation on my previous bug (https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100917) I would welcome validation and votes. class ISkewEstimator {
11
4697
by: Dominic Vella | last post by:
I am using MS-Access2000. I can't seem to set the default values for Logical type fields. I start with Dim dbsTmp As Object ' I think it's DAO.Database Set dbsTmp = DBEngine.OpenDatabase(CurrentProject.path & "\data_be.mdb") and then use the following to run my SQL ststement dbsTmp.Execute ----------------------------------------------------- Something like this SQL statement works: "ALTER TABLE tblStudent ADD COLUMN student_number...
6
2469
by: Tarique | last post by:
Can someone please explain the idea of Logical Constness as explained in section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a short working code if possible. Thank You
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.