-3

given the following C++ code:

class XY
{
public:
    int p;
    XY(int i):p{i}
    {}

    
    XY& operator=(const XY& cs)
    {
        if (==nullptr) //????
        {
            p=0;
            return *this;
        }
        
        ...
        return *this;
    }

};


int main ()
{
    XY a=3;
    XY *s=nullptr;
    a=*s;
}

a is type of XY and s is a pointer of this class that points to null. How can I program the assignment-operator so that if s is a null pointer, that a.p=0?

  • 5
    You can't. You don't have any influce on the dereferencing `*s`. You *could* overload the assignment operator to take a pointer, which I would not recommend. – Lukas-T Sep 01 '23 at 13:53
  • 3
    `cs` is not a `nullptr`. References can't be null. – Yksisarvinen Sep 01 '23 at 13:54
  • How about `XY& operator=(XY const*)`? But not something I recommend. And you need to assign using the *pointer*, not dereference it. – Some programmer dude Sep 01 '23 at 13:57
  • 1
    With that said, this feels very much as a [XY problem](https://en.wikipedia.org/wiki/XY_problem). Why do you need to use pointers? Why do you need to detect null pointers? What is the actual and underlying problem you're trying to solve? – Some programmer dude Sep 01 '23 at 13:59
  • You could overload the operator to take a `nullptr_t`, but yeah, why? – Etienne de Martel Sep 01 '23 at 14:04
  • Why are you attached to writing `a = *s` when `s` is a null pointer? What does `*s` even mean when `s` is a null pointer? What do you hope/think will be passed as an argument to the assignment operator as the `const XY& cs` argument after being given `*s` when `s` is a nullptr? You may _think_ this means something in C++ but the behaviour is not defined. – Wyck Sep 01 '23 at 14:11
  • @Someprogrammerdude This is definitely an XY problem. The class is even called `XY`! ;) – Wyck Sep 01 '23 at 14:12
  • 2
    When there's a bug in a program that results in a null pointer and that causes a crash, the correct fix is not to avoid or work around the crash, but to figure out the bug that resulted in a null pointer. – Sam Varshavchik Sep 01 '23 at 14:16
  • Either a reference is valid, or a reference is **undefined behavior**. A reference cannot ever validly be **null**. Hence, there is no need to check to ensure a reference is not a `nullptr`. (That's C++. In some other languages, references can be null. Those languages have *different semantics* for what constitutes a "reference".) – Eljay Sep 01 '23 at 14:25

1 Answers1

2

First of all, XY *s = nullptr; does not perform assignment, it performs copy-initialization. In that scenario, operator= is not called. a = *s; dereferences the null pointer and binds a reference to the result, which is undefined behavior.

In your code ...

XY& operator=(const XY& cs)
{
    if (==nullptr) //????

... the null pointer check is unnecessary. References cannot be null in C++, only pointers can be.

While it is undefined behavior, you might be able to diagnose this undefined behavior by checking

if (&cs == nullptr)

However, since a compiler knows that references cannot refer to a "null object", there is no guarantee that this check won't be optimized away by the compiler.

See Also

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96