www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - opAssign and classes

reply Bill Baxter <dnewsgroup billbaxter.com> writes:
I wrote a class with an opAssign.
But I find I keep getting bitten by this:

     MyType x = value;

When I should be typing:

     MyType x = new MyType;
     x = value;

Somehow I just keep forgetting to do that.
Then I get a nice nondescript "Error: Access violation" at some unknown 
point in my program that I have to track down.

Anyone else find themselves making this kind of error?
Any thoughts for how I or the compiler could help protect against this 
kind of mistake?

I thought I could stick a check for 'this is null' in the opAssign 
method but that doesn't help because apparently it's dereferencing the 
null pointer to look up opAssign's address that causes the Error, not 
what happens in the call itself.

--bb
Dec 24 2006
parent BCS <BCS pathilink.com> writes:
Bill Baxter wrote:
 I wrote a class with an opAssign.
 But I find I keep getting bitten by this:
 
     MyType x = value;
 
 When I should be typing:
 
     MyType x = new MyType;
     x = value;
 
 Somehow I just keep forgetting to do that.
 Then I get a nice nondescript "Error: Access violation" at some unknown 
 point in my program that I have to track down.
 
 Anyone else find themselves making this kind of error?
 Any thoughts for how I or the compiler could help protect against this 
 kind of mistake?
 

1> convert a seg-v to an exception (signal ??) 2> implement poor man's stack tracing: T fn() { debug scope(failure) writef(__FILE__":"~itoa!(__LINE__)); ... }
 I thought I could stick a check for 'this is null' in the opAssign 
 method but that doesn't help because apparently it's dereferencing the 
 null pointer to look up opAssign's address that causes the Error, not 
 what happens in the call itself.
 
 --bb

Dec 24 2006