Transitive Const in OO programming

Alex Burton alexibu at mac.com
Wed Aug 8 16:48:05 PDT 2007


> Regan Heath Wrote:
> 
> > Correct me if I'm wrong but you want to be able to exclude parts of your 
> > class from the protection given by 'const', specifically:
> 
No what I want is to have StateMachineWrapper observe and manipulate StateMachine without StateMachine being considered part of StateMachineWrapper

The StateMachine could be a TCP/IP socket or some other communications channel where in order to get a result you must send a message.

There are three concepts embodied in a pointer 'has a reference to' and 'is part of' and 'ownership'.
In C++ if you had a reference to an object, the latter two concepts were orthogonal, and could be added or subtracted to best reflect reality.
In D as soon as you have a reference to an object the latter two concepts are assumed. I am trying to understand how to make this work in real software.

The only solutions I can see at the moment are:
To make getResult non const which will leak into any calling classes and will effectively make the whole program impossible to get const correct.

To do a const cast somewhere as you have shown - not really ideal - and causes undefined behaviour according to docs.

To separate object oriented code with a C style interface wherever this occurs. Essentially getting around const by using static functions and token references - like the c standard library FILE fopen fclose functions.
This option is really ugly too.

To assume that any real statemachines are always outside the scope of D and done in some low level library written in another language. Not a good policy for a language like D I would think.

Alex



More information about the Digitalmars-d mailing list