Initialize Pointer Through Function

Posted by SoulBeaver on Stack Overflow See other posts from Stack Overflow or by SoulBeaver
Published on 2010-06-10T06:01:41Z Indexed on 2010/06/10 6:02 UTC
Read the original article Hit count: 376

Filed under:
|

I was browsing my teacher's code when I stumbled across this:

Order* order1 = NULL;

then

order1 = order(customer1, product2);

which calls

Order* order(Customer* customer, Product* product)
{
    return new Order(customer, product);
}

This looks like silly code. I'm not sure why, but the teacher initialized all pointers to NULL instead of declaring them right away(looking at the code it's entirely possible, but he chose not to).

My question is: is this good or acceptable code? Does the function call have any benefits over calling a constructor explicitely? And how does new work in this case? Can I imagine the code now as kind of like:

order1 = new Order(customer, product);

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointers