Afficher un message
Vieux 15/01/2008, 22h34   #6
Salt_Peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why pA->foo() works in this code?

On Jan 15, 1:40 pm, "Luis Angel Fdez. Fdez." <laff...@gmail.com>
wrote:
> El Tue, 15 Jan 2008 08:15:40 -0800, tragomaskhalos escribió:
>
> Hi, again.
>
> Thank you all for your anwsers.
>
> >> void MakeA(A *pA) { pA = new A; }

>
> > This just sets pA and then forgets it value: you mean void MakeA(A*& pa)
> > { pA = new A; }

>
> That's what we thought, but when we saw it working (or doing something
> it seemed right) we were surprised.


It doesn't forget its value, the pointer is passed by value. In other
words, the pointer pA in main() is indeed pointing to a valid new
allocation upon function return. Of course, there is a syntax error in
main():
Make(pA);
should be
MakeA(pA);

as far as the following is concerned, ask yourself: is a constructor
of any kind invoked? no, hence undefined behaviour.

>
> >> And... why ((A*)NULL)->foo() works too?

>
> > This came up recently, see:
> >http://groups.google.co.uk/group/com...thread/thread/
> > a807522e71a60c8e/f4afa72f427ed638?hl=en

>
> Thank you. I was searching something about that, but I couldn't find
> anything.
>
> Bye.
>
> --
> Ubuntu 7.10 (gutsy) (kernel 2.6.22-14-generic i686) GNOME 2.20.1
> Intel(R) Core(TM)2 Quad CPU (2260.245 MHz) up 33 min, 2 users
> HT: Zanzabornín (1457021) X.1762 #http://muxin.no-ip.org/
> SK: C.D. Arrancatapinos (18088) IV.57 # Jabber: laff...@gmail.com


  Réponse avec citation
 
Page generated in 0,05321 seconds with 9 queries