Afficher un message
Vieux 15/01/2008, 17h02   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why pA->foo() works in this code?

Luis Angel Fernandez Fernandez wrote:
> why this code works?
>
> #include <iostream>
>
> class A {
> public:
> void foo() {std::cout << "hi" << std::endl; };
> };
>
> void MakeA(A *pA) { pA = new A; }
>
> int main() {
> A* pA = NULL;
> Make(pA);
> pA->foo();
> delete pA;
>
> return 0;
> }
>
> And... why ((A*)NULL)->foo() works too?


The code has undefined behaviour (UB). One of the results of UB
is the appearance of being "working". Read up on UB and don't
write code like that.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


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