Error: 'this' is only defined in non-static member functions, not parse

Matej Nanut matejnanut at gmail.com
Tue Jan 17 09:58:55 PST 2012


On 17 January 2012 18:29, Timon Gehr <timon.gehr at gmx.ch> wrote:
> I'm quite sure that the error in your code occurs for the same reason as in
> the following code snippet:
>
> class C{
>    class D{}
>    static make(){return new D();} // error
> }
>
> You can fix it by making D static:
>
> class C{
>    static class D{}
>    static make(){return new D();} // ok
> }
>
> The reason is that non-static inner classes have an implicit 'outer'
> property that links to the class it was created with. Therefore, to
> construct them inside a member function, the implicit 'this' pointer is
> needed. If the 'outer' property is actually unwanted, it is best to declare
> inner classes as static.

Yes! If I move the class and its subclasses out of its outer class, and declare
them all static, it works!

Note that your `make' function is being called within class `D' in my example,
if I replace the names. However, the same thing applies.

Your explanation was nice, but now I'd like to know what the difference of a
non-static vs. a static class is, if they're defined top-level? Or are they then
the same? I don't expect anyone to thoroughly explain things to me, but
pointing out a good source, like a link or a book, would be really helpful.

I lack general knowledge in the OOP area and must really learn more about
it, as I've always been programming in C and could easily get away with it
as we were doing small-ish programs at university.


More information about the Digitalmars-d-learn mailing list