Bug 830 – Access to static member variable causes Access Violation
Status
RESOLVED
Resolution
INVALID
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-01-11T06:45:00Z
Last change time
2014-02-15T13:12:37Z
Assigned to
bugzilla
Creator
ralfs72
Comments
Comment #0 by ralfs72 — 2007-01-11T06:45:18Z
void main(char[][] args)
{
class A
{
static int v = 1;
int getv()
{
return v;
}
}
A a;
assert (a.getv() == 1);
}
==> Access Violation
Comment #1 by davidl — 2007-01-11T07:22:54Z
this ain't a bug. coz getv ain't static
Comment #2 by ralfs72 — 2007-01-11T07:47:48Z
(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());
}
Comment #3 by tomas — 2007-01-11T07:50:59Z
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);
}
Comment #4 by ralfs72 — 2007-01-11T08:01:18Z
(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