Lets say I have the following class
struct FooList {
int data;
FooList *next;
// basic constructor
FooList(int d, FooList *n): data(d), next(n) {}
// basic destructor
~FooList() { if (next) delete next; }
FooList *functional_append(FooList *) const;
};
And then in my implementation, I define
FooList *FooList::functional_append(FooList *other) const {
if (this == NULL)
return other;
else
return new FooList(data, next->functional_append(other));
}
Does the condition on the second line make any sense? Will this
ever be NULL
? I know that if functional_append
was declared virtual
then this would throw a segmentation fault once this == NULL
, but as a simple member function would it still work like I expect it to?
Note: I am looking for a reason why the above will or will not work (like the equivalent C code), not just "No, it can't happen" or "Yes, if force it to like this ...".