Thread overview
[Issue 830] New: Access to static member variable causes Access Violation
Jan 11, 2007
d-bugmail
Jan 11, 2007
d-bugmail
Jan 11, 2007
d-bugmail
Jan 11, 2007
d-bugmail
Jan 11, 2007
d-bugmail
January 11, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=830

           Summary: Access to static member variable causes Access Violation
           Product: D
           Version: 1.00
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: ralfs72@gmx.net


void main(char[][] args)
{
        class A
        {
                static int v = 1;

                int getv()
                {
                        return v;
                }
        }

        A a;
        assert (a.getv() == 1);
}

==> Access Violation


-- 

January 11, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=830





------- Comment #1 from davidl@126.com  2007-01-11 07:22 -------
this ain't a bug. coz getv ain't static


-- 

January 11, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=830





------- Comment #2 from ralfs72@gmx.net  2007-01-11 07:47 -------
(In reply to comment #1)
> this ain't a bug. coz getv ain't static
> 

And? What's the problem with it? Is it not possible to access static vars in a member function?

Of course the opposite way can not work: Access non static member vars from a static member function.

Well the equivalent C++ programm works:

class A
{
public:
        static int v;

        int getv()
        {
                return v;
        }
};

int A::v = 1;

int _tmain(int argc, _TCHAR* argv[])
{
        A a;

        printf("%d\n", a.getv());
}


-- 

January 11, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=830





------- Comment #3 from tomas@famolsen.dk  2007-01-11 07:50 -------
your instance of A is null

This should work:

void main(char[][] args)
{
        class A
        {
                static int v = 1;

                int getv()
                {
                        return v;
                }
        }

        A a = new A;
        assert (a.getv() == 1);
}


-- 

January 11, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=830


ralfs72@gmx.net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #4 from ralfs72@gmx.net  2007-01-11 08:01 -------
(In reply to comment #3)
> your instance of A is null
> 
> This should work:
> 
> void main(char[][] args)
> {
>         class A
>         {
>                 static int v = 1;
> 
>                 int getv()
>                 {
>                         return v;
>                 }
>         }
> 
>         A a = new A;
>         assert (a.getv() == 1);
> }
> 

OHH! Sorry! I have to get used to that classes are allways references!

Sorry again! - I put it to INVALID


--