digitalmars.D.learn - Can't create nested classes
- Heinz (11/11) Jan 22 2007 I have the following code, i'm working with nested classes, when i try t...
- Jarrett Billingsley (4/5) Jan 22 2007 You're not allowed to have non-constant expressions in class member
- Heinz (21/31) Jan 22 2007 Check this out, if i write this code it works:
- Heinz (3/13) Jan 23 2007 I can't also have variables like 'int i', it throws access violation. Bu...
- Alexander Panek (3/6) Jan 23 2007 No offense intended, but I do think you're doing something wrong here.
- Heinz (4/11) Jan 23 2007 Sure man, attached is the code with the main file included, just compile...
- Bradley Smith (11/26) Jan 23 2007 The crash in the example code isn't from nested classes. It is from the
I have the following code, i'm working with nested classes, when i try to compile it it enters a nice loop full of errors: class myclass { private Table mytable = new Table(); class Table { } } I get errors about a 'this' so i add this() constructors to myclass, Table and tried both at the same time. What could be wrong? thx
Jan 22 2007
"Heinz" <billgates microsoft.com> wrote in message news:ep408i$2389$1 digitaldaemon.com...private Table mytable = new Table();You're not allowed to have non-constant expressions in class member initializers. Move this 'new' into a constructor for myclass.
Jan 22 2007
Jarrett Billingsley Wrote:"Heinz" <billgates microsoft.com> wrote in message news:ep408i$2389$1 digitaldaemon.com...Check this out, if i write this code it works: class myclass { private Table mytable; this() { mytable = new Table(); } } but if i write this code i get an access violation: class myclass { private Table mytable, mytable2; this() { mytable = new Table(); mytable2 = new Table(); } } And of course i need the second block of code.private Table mytable = new Table();You're not allowed to have non-constant expressions in class member initializers. Move this 'new' into a constructor for myclass.
Jan 22 2007
Jarrett Billingsley Wrote:"Heinz" <billgates microsoft.com> wrote in message news:ep408i$2389$1 digitaldaemon.com...I can't also have variables like 'int i', it throws access violation. But why in the DMD page here http://www.digitalmars.com/d/class.html is listed a bunch of classes with 1 to 3 non-static members like int i?private Table mytable = new Table();You're not allowed to have non-constant expressions in class member initializers. Move this 'new' into a constructor for myclass.
Jan 23 2007
Heinz wrote:I can't also have variables like 'int i', it throws access violation. But why in the DMD page here http://www.digitalmars.com/d/class.html is listed a bunch of classes with 1 to 3 non-static members like int i?No offense intended, but I do think you're doing something wrong here. Would you mind pasting the whole source code, please?
Jan 23 2007
Alexander Panek Wrote:Heinz wrote:Sure man, attached is the code with the main file included, just compile with these 2 files and you're ready. I noticed if i remove all references to type File or delegate, it works ok. I mean, you can have the delegate or the File but not both. Good luckI can't also have variables like 'int i', it throws access violation. But why in the DMD page here http://www.digitalmars.com/d/class.html is listed a bunch of classes with 1 to 3 non-static members like int i?No offense intended, but I do think you're doing something wrong here. Would you mind pasting the whole source code, please?
Jan 23 2007
The crash in the example code isn't from nested classes. It is from the destructor of class Writer. The destructor is calling a method on the garbage collected member object "testFile". This reference is no longer valid. From http://www.digitalmars.com/d/class.html#destructors "When the garbage collector calls a destructor for an object of a class that has members that are references to garbage collected objects, those references are no longer valid." In addition, the call is unnecessary because the class File will call close() in its destructor. Heinz wrote:Alexander Panek Wrote:Heinz wrote:Sure man, attached is the code with the main file included, just compile with these 2 files and you're ready. I noticed if i remove all references to type File or delegate, it works ok. I mean, you can have the delegate or the File but not both. Good luckI can't also have variables like 'int i', it throws access violation. But why in the DMD page here http://www.digitalmars.com/d/class.html is listed a bunch of classes with 1 to 3 non-static members like int i?No offense intended, but I do think you're doing something wrong here. Would you mind pasting the whole source code, please?
Jan 23 2007