const challenge

Steven Schveighoffer schveiguy at yahoo.com
Thu Jan 31 10:21:26 PST 2008


"Jason House" wrote
> Jason House Wrote:
>
>> 1. Create a usage example that can only be solved by stripping const out, 
>> or adopting bad coding practices (global variables?).  I suspect this may 
>> bring back old examples about why transitive const is bad.  Please avoid 
>> flames such as "const is useless" or "my favorite language does not have 
>> const and it's great"
>
>
> Here's an example I remember seeing posted.  IMHO, it sounds like a rather 
> annoying thing to handle.  It can be solved by stripping out the const 
> references to class A or possibly replacing "executionLog x" with some 
> global variable reference.  I could imagine this being problematic with 
> multi-threaded or other large applications that have distinct logs for 
> different components/threads.
>
> class A{
>  version(debug){
>    executionLog x;
>  }
>   ... // includes some writing to log for debugging
> }
> class B{
>  const class A; // can't work while debugging
>  ...
> }

The obvious solution is to store the execution log outside the A instance. 
However, I'm not sure how you use it, or if you need to have multiple 
instances of x.  Normally logging is done through a static/global variable, 
i.e.:

class A {
  version(debug){
    static executionLog x;
    static this()
    {
        x = getExecutionLogForThisClass();
    }
  }
}

-Steve 





More information about the Digitalmars-d mailing list