-3

I have function that returns pointer to child object.

Function prototype:

ObjClass* ObjClass::getChildFromParent(ObjClass* parent = nullptr);

This function has default parameter as null pointer and I want to check this parameter anyway, because if pointer is null I will return nullptr but if it's not I will return pointer to child object, so:

ObjClass* ObjClass::getChildFromParent(ObjClass* parent)
{
    return parent == 0 ? nullptr : parent->getChild();
}

But I think checking null pointer using zero is not correct. So, should I use keyword to determine null pointer?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
WideWood
  • 549
  • 5
  • 13
  • I prefer ternary operators. – WideWood Apr 25 '21 at 19:11
  • Because ObjClass is not only what I use that't why I return ```nullptr``` – WideWood Apr 25 '21 at 19:12
  • Fyi, `ObjClass* ObjClass::getChildFromParent(ObjClass* parent = nullptr)` in the implementation is not correct because you already specified the default value in the prototype. You should only do it once, and that's where you do it (the prototype, if separate from the implementation). as shown you should be getting a "redefinition of default argument" error (at least in C++14 and prior mode). – WhozCraig Apr 25 '21 at 19:13
  • Thanks but I am with old C++ standard it's something like legacy code. – WideWood Apr 25 '21 at 19:14
  • 3
    @WideWood _"Thanks but I am with old C++ standard"_ Eeerm, so does that standard have `nullptr` or not? – πάντα ῥεῖ Apr 25 '21 at 19:17
  • Oops of course I can't define parameter as null in implementation. – WideWood Apr 25 '21 at 19:20
  • No, parent is private property of this (ObjClass) class as well as 'child' object. – WideWood Apr 25 '21 at 19:24

4 Answers4

1

Just compare it to nullptr:

return parent == nullptr ? nullptr : parent->getChild();

You can also use the contextual cast to bool:

return parent ? parent->getChild() : nullptr;
bolov
  • 72,283
  • 15
  • 145
  • 224
1

According to the C++ 14 Standard (5.10 Equality operators)

2 If at least one of the operands is a pointer, pointer conversions (4.10) and qualification conversions (4.4) are performed on both operands to bring them to their composite pointer type (Clause 5). Comparing pointers is defined as follows: Two pointers compare equal if they are both null, both point to the same function, or both represent the same address (3.9.2), otherwise they compare unequal.

And (4.10 Pointer conversions)

1 A null pointer constant is an integer literal (2.13.2) with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion...

Thus the expression in the return statement

 return parent == 0 ? nullptr : parent->getChild();

is entirely correct because the null pointer constant 0 is converted to a null pointer value of the type of the pointer parent. But it will be more expressive to write

 return parent == nullptr ? nullptr : parent->getChild();
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

I'd prefer to write

ObjClass* ObjClass::getChildFromParent(ObjClass* parent = nullptr) {
    if(parent) 
        return parent->getChild();
    return nullptr;
}

That's less confusing.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
-1

It's more correct to use nullptr keyword to declare null pointer and check it, for example:

int* example_ptr(0); // example_ptr is null pointer now

int ptr; // ptr is not initialized pointer 

And we can make ptr null pointer using assignment 0

ptr = 0; // ptr is null pointer now

You should initialize pointer using zero if you won't assign other values to it. But in different cases it's better to use nullptr keyword beacuse compiler can't understand what zero is (integer value or null pointer) but it happens not always. Also this keyword is better than NULL macro because it is defined like zero too.

Well I should check null pointer like so:

ObjClass* ObjClass::getChildFromParent(ObjClass* parent)
{
    return parent == nullptr ? nullptr : parent->getChild();
}
WideWood
  • 549
  • 5
  • 13