www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can't create nested classes

reply Heinz <billgates microsoft.com> writes:
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
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
next sibling parent Heinz <billgates microsoft.com> writes:
Jarrett Billingsley Wrote:

 
 "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.
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.
Jan 22 2007
prev sibling parent reply Heinz <billgates microsoft.com> writes:
Jarrett Billingsley Wrote:

 
 "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.
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?
Jan 23 2007
parent reply Alexander Panek <a.panek brainsware.org> writes:
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
parent reply Heinz <billgates microsoft.com> writes:
Alexander Panek Wrote:

 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?
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 luck
Jan 23 2007
parent Bradley Smith <digitalmars-com baysmith.com> writes:
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:
 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?
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 luck
Jan 23 2007