C++ frequently questioned answers
Bruce Adams
tortoise_74 at yeah.who.co.uk
Sat Oct 27 16:42:34 PDT 2007
Bruno Medeiros Wrote:
> Bruce Adams wrote:
> > Bruno Medeiros Wrote:
> >
> >> Bruce Adams wrote:
> >>> [OT]
> >>> Someone at work pointed out this java chestnut recently. If you've used java much you will know it but it may come as a shock to a mainly C++ coder.
> >>>
> >>> class foo {
> >>> bar x;
> >>> void foo () {
> >>> x = new bar();
> >>> }
> >>> }
> >>>
> >>> foo is NOT a constructor its actually a method named foo. The java compiler or interpreter generates a default constructor that sets all members to null so any use of x in any method of foo will result in a null pointer exception. This apparently compiles with no warnings.
> >>> [/OT]
> >> What's so surprising about that, even for a Java newcomer?
> >>
> >> --
> >> Bruno Medeiros - MSc in CS/E student
> >> http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
> >
> > Its the fact that a simple typo can cause such mayhem. Remove the void from foo() and it becomes a constructor and everthing works just fine.
> >
> >
>
> No one in the Java world writes classes with lowercase first letter
> anyways. :P
>
> --
> Bruno Medeiros - MSc in CS/E student
> http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Oh no! My example is now completely undermined and devalued!
More information about the Digitalmars-d
mailing list