December 25, 2006 opAssign and classes | ||||
---|---|---|---|---|
| ||||
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 |
December 25, 2006 Re: opAssign and classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | 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 |
Copyright © 1999-2021 by the D Language Foundation