[Issue 622] New: There should be a warning for unininitalized class reference

Ary Manzana ary at esperanto.org.ar
Thu Nov 30 16:51:10 PST 2006


d-bugmail at puremagic.com escribió:
> http://d.puremagic.com/issues/show_bug.cgi?id=622
> 
>            Summary: There should be a warning for unininitalized class
>                     reference
>            Product: D
>            Version: 0.175
>           Platform: PC
>         OS/Version: Windows
>             Status: NEW
>           Severity: enhancement
>           Priority: P2
>          Component: DMD
>         AssignedTo: bugzilla at digitalmars.com
>         ReportedBy: andkhropov at mtu-net.ru
> 
> 
> This error is very common for people with C++ background (I've already seen
> people asking questions about similar code several times):
> ----------------------------------------------
> class Foo{
>   public int data;
> }
> 
> void main()
> {
>   Foo f; // obviously they try do this way as they got used to it in C++
>          // should be a warning here!
> 
>   f.data = 1;// Access violation
> }
> 
> ----------------------------------------------
> 
> 

Actually, the warning should be in "f.data = 1;". You are trying to 
access a member of an uninitialized variable. Take a look at this code:

----------------------------------------------
Foo f;
if (something) {
     f = new Foo();
} else {
     f = new Bar();
}

f.data = 1;
----------------------------------------------

(where Bar extends Foo, for example). No warning should be generated, 
everyhing in it's place. That's the way access to uninitialized variable 
in java works. They are errors, not just warnings (the program will 
crash there, no matter what happens).



More information about the Digitalmars-d-bugs mailing list