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
|