Access violation when using classes (beginner)
Sean Kelly
sean at f4.ca
Fri Jan 5 20:57:20 PST 2007
Robin Allen wrote:
> Hi, could someone tell me why this code gives an access violation? It seems I
> must be missing some fundamental difference between C++ and D.
>
> class C
> {
> void zing() {}
> }
>
> void main()
> {
> C c;
> c.zing();
> }
Using objects in D is similar to using them in Java in that objects are
reference-based. Try:
void main()
{
// Will be collected by the GC at some point after c goes
// out of scope.
C c = new C();
c.zing();
// Will be destroyed automatically the moment d goes out of
// scope. The class instance may be allocated on the stack
// but is not required to be.
scope C d = new C();
d.zing();
}
Sean
More information about the Digitalmars-d-learn
mailing list