0

In the following example:

template<class Foo>
struct FooBar 
{
   FooBar(Foo *pObj = 0) : pFoo_(pObj) {}
};

what does "*pObj = 0" mean?

Sebi
  • 4,262
  • 13
  • 60
  • 116
  • So basically pObj is set to NULL prior to being used? – Sebi Aug 18 '12 at 15:09
  • 2
    IIRC, it means that it will default to NULL if nothing is passed in. – nhahtdh Aug 18 '12 at 15:10
  • @Sebi: If you don't pass any parameters to the constructor then it will use `0` (aka null pointer) as the value of `pObj`. Note this has nothing to do with templates it is just normal C++ syntax for automatic parameters. – Martin York Aug 18 '12 at 15:41

1 Answers1

2

It means that the default value of pObj, if the caller doesn't provide one, is 0. In this particular case, it would have been better form to use NULL (which is usually a macro of 0). There are now two ways of calling it:

FooBar fb = FooBar(); //pObj is NULL
FooBar fb2 = FooBar(someFoo); //pObj is someFoo
Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • 2
    NULL cannot be a macro for `(void*)0` in C++, because the compiler will refuse to implicitly cast `void*` into another pointer type, unlike in C. – BatchyX Aug 18 '12 at 15:12
  • 2
    `FooBar fb();` is not null but `fb` is function – Mr.Anubis Aug 18 '12 at 15:13
  • @Mr.Anubis: Fixed most vexing parse issue. – Linuxios Aug 18 '12 at 15:16
  • Personally I prefer using `NULL`. But I don't think it is bad form to use `0` (but I will move to `nullptr` when it get here). Further reading: [here](http://stackoverflow.com/q/176989/14065), [here](http://stackoverflow.com/q/1296843/14065) and [here](http://c-faq.com/null/nullor0.html) links doug up by [Konrad](http://codereview.stackexchange.com/users/308/konrad-rudolph) on a question we were discussing [here](http://codereview.stackexchange.com/a/13062/507) – Martin York Aug 18 '12 at 15:36
  • @LokiAstari: Thanks for the links. I just think that `NULL` can be a little clearer because it makes it clear that you are working with pointers in an intentional way -- not just creating buckets of segfaults. – Linuxios Aug 18 '12 at 15:41
  • @Linuxios: I totally agree with you. Personally I think NULL is preferable. But I would not go all the way to say that all the C++ community agrees with me/you, this is one of those debated topics (whose outcome I don't care about as nullptr is coming). – Martin York Aug 18 '12 at 15:44
  • And note that, despite the title, this is not about templates. The same thing applies to non-template functions: `void f(int i = 0);' declares a function that takes one argument of type `int` and returns `void`. It can be called with no arguments; in that case, the value 0 is passed as the argument. – Pete Becker Aug 18 '12 at 22:29
  • @pete I have ran across this example in a template context. Anyway thanks for the downvote. – Sebi Aug 20 '12 at 15:25
  • @Sebi - I don't know what you're referring to. I didn't downvote. I just commented that this applies in a much broader context. – Pete Becker Aug 20 '12 at 21:56